Contenu connexe Similaire à Objectie-C de ラムダ Similaire à Objectie-C de ラムダ (20) Plus de Akihiko Matuura (6) Objectie-C de ラムダ12. #import <Foundation/Foundation.h>
// クラスの宣言
@interface MyClass : NSObject {
int val;
}
- (id)init;
+ (void)classMethod:(id)arg; // クラスメソッド
- (id)method:(NSObject*)arg1 with:(int)arg2;
@end
// 実装
@implementation MyClass
+ (void)classMethod:(id)arg {
// some operation
}
- (id)method:(NSObject*)arg1 with:(int)args2 {
return obj;
}
@end
10
13年9月4日水曜日
13. #import <Foundation/Foundation.h>
// クラスの宣言
@interface MyClass : NSObject {
int val;
}
- (id)init;
+ (void)classMethod:(id)arg; // クラスメソッド
- (id)method:(NSObject*)arg1 with:(int)arg2;
@end
// 実装
@implementation MyClass
+ (void)classMethod:(id)arg {
// some operation
}
- (id)method:(NSObject*)arg1 with:(int)args2 {
return obj;
}
@end
10
13年9月4日水曜日
17. #import <Foundation/Foundation.h>
#include <iostream>
#include <boost/shared_ptr.hpp>
class CppClass{
public:
C++のクラス
! CppClass(){}
! void print() const {
! ! std::cout << "CppClass::print" << std::endl;
! }
};
@interface ObjcClass : NSObject
- (void)print;
@end
Objective-Cのクラス
@implementation ObjcClass
- (void)print {
! NSLog(@"ObjcClass print");
}
@end
void boost_test(){
! boost::shared_ptr<CppClass> p(new CppClass);!
! ObjcClass* oc = [[ObjcClass alloc]init];!
! [oc print]; // ObjcClass print
! p->print(); // CppClass::print
}
14
13年9月4日水曜日
19. 参照カウンタ
• 難敵:retain,release,autorelease
MyObject* obj = [[MyObject alloc] init]; // 参照カウント1
[obj retain]; // 参照カウント2
NSLog(@"count=%d", [obj retainCount] ); // count=2
[obj release] ; // 参照カウント1
[obj release] ; // 参照カウント0,開放される
・・・
// メモリプールが有効なあいだ生きてる
MyObject* obj = [[[MyObject alloc] init] autorelease];
[obj retain]; // 参照カウント2、メモリプールの生き死に関わらず保持した
い!
16
13年9月4日水曜日
21. Automatic Reference
Counting (ARC)
• iOS5、MacOSX10.6から
• Xcode4.2ではデフォルト
• 参照カウンタ管理をコンパイル時に解決し
てくれる。ヽ(^。^)ノ
• retain/release/retainCount等は書かなく
ていい。
•
てか、書いたらコンパイルエラー orz
• GCとは別(GCもオプションであり)
18
13年9月4日水曜日
27. Using a Block
void (^b1)(int) = ^(int x){NSLog(@"Hello Blocks %d",x);};
b1(100); // Hello Blocks 100
// パラメータ無しなら定義側は()を省略してもOK
void (^b2)() = ^{NSLog(@"Hello Blocks 2");};
b2(); // Hello Blocks 2
24
13年9月4日水曜日
32. キャプチャの例
int g=999; // グローバル変数
・・・
int x = 100;
void (^b)() = ^(){NSLog(@"x=%d g=%d",x,g);};
b(); // x=100 g=999
x=200;
b(); // x=100(変わらず)
g=888;
b(); // x=100 g=888
29
13年9月4日水曜日
34. クラスで使う
@interface testClass : NSObject{
int x_;
}
-(void) myprint;
-(void) print:(void(^)(int x))printer;
@end
@implementation testClass
-(void)print:(void (^)(int x))printer {
printer(x_);
}
testClass* t = [[testClass alloc]init];
[t print:^(int x){ NSLog(@"my printer x=%d", x);}];
31
13年9月4日水曜日
35. selfのキャプチャ
-(void)myprint { // testClassのメソッド
x_ = 100;
void(^b)()=^{ NSLog(@"x_=%d", x_);};
b(); // x_ = 100
x_ = 200;
b(); // x_ = 200 !!(コピーと違うの??)
// こう書いているのと同じ
}
void(^b2)()=^{ NSLog(@"testClass x_=%d", self->x_);};
• メンバー変数がキャプチャされているのではない
• selfがキャプチャされている!
32
13年9月4日水曜日
36. 高階関数、部分適用
C#
Func<int, Func<int, int>> f = x => y => x + y;
Func<int, int> fc = f(1);
int x = fc(3); // 4 (1 + 3)
C++11
auto f = [](int x){ return [x](int y){return x+y;}; };
auto fc = f(1);
int x = fc(3); // 4 (1 + 3)
33
13年9月4日水曜日
37. Blockだと。。
void blocks_test()
{
! typedef int (^add_function)(int);
! add_function (^f)(int) =
! ! ! ! ! ^(int x){
! ! ! ! ! ! return Block_copy(^(int y){ return x+y;});
! !
! ! ! };
! add_function fc = f(1);
! NSLog(@"blocks_test() fc=%d", fc(3)); // 4 (1 + 3)
! Block_release(fc);
}
34
13年9月4日水曜日
39. ARCありなら...
void blocks_test()
{
! typedef int (^add_function)(int);
! add_function (^f)(int) =
^(int x){return ^(int y){return x+y;};};
! add_function fc = f(1);
! NSLog(@"blocks_test() fc=%d", fc(3)); // 4 (1 + 3)
}
すっきり(^^)
36
13年9月4日水曜日
43. 簡易function作った
template<typename R, typename ...V>
struct my_function {
struct HolderBase{
virtual R run(V... arg)=0;
};
template<typename F>
struct Holder : HolderBase {
F f_;
Holder(F f):f_(f){}
virtual R run(V... arg) {
return f_(arg...);
}
};
boost::scoped_ptr<HolderBase> holder_;
template<typename F>
my_function(F f) : holder_(new Holder<F>(f) ){}
R operator()(V... arg) {
holder_->run(arg...);
}
};
40
13年9月4日水曜日
45. C++11 autoで型推論
// blockをautoで型推論
auto af = ^(int x){NSLog(@"Hello C++11 auto!! x=%d",x);};
af(5); // Hello C++11 auto!! x=5
// 高階関数も楽チン
auto f = ^(int x){ return ^(int y){ return x+y;};};
auto fc = f(1);
NSLog(@"Hello C++11 auto!! fc=%d",fc(3)); // fc=4
42
13年9月4日水曜日
46. C++11 autoで型推論
// blockをautoで型推論
auto af = ^(int x){NSLog(@"Hello C++11 auto!! x=%d",x);};
af(5); // Hello C++11 auto!! x=5
// 高階関数も楽チン
auto f = ^(int x){ return ^(int y){ return x+y;};};
auto fc = f(1);
NSLog(@"Hello C++11 auto!! fc=%d",fc(3)); // fc=4
やっぱC++かわいいw
42
13年9月4日水曜日