More Related Content Similar to Objc lambda (20) Objc lambda12. #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. #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
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
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
21. Automatic Reference
Counting (ARC)
• iOS5、MacOSX10.6から
• Xcode4.2ではデフォルト
• 参照カウンタ管理をコンパイル時に解決し
てくれる。ヽ(^。^)ノ
• retain/release/retainCount等は書かなく
ていい。
• てか、書いたらコンパイルエラー orz
• GCとは別(GCもオプションであり)
18
22. その他の機能
• ランタイムシステム
• プロパティ
• プロトコル
• カテゴリ
• クラスエクステンション
• クラスクラスタ
• 例外
• GC
• 高速列挙
• ・・・などなど、あと、フレームワークには一切触れてません
19
25. Blocksって
• ようはラムダです。
• ラムダ式のリテラル
• MacOSX 10.6/iOS 4.0以降
• AppleのC言語の拡張
• http://www.open-std.org/jtc1/sc22/wg14/
www/docs/n1370.pdf
22
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
30. Blocks応用
といっても、
キャプチャとかBlockの寿命とかそのへん
27
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
33. ミュータブルで共有なキャ
プチャ
__block int x=100;
void (^b)() = ^(){NSLog(@"x=%d g=%d", x++, g);};
b(); // x=100 g=999
b(); // x=101 g=999
x=200;
b(); // x=200 g=999
30
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
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
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
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
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
42. C++11のfunction<T>
にBlockを
std::function<void(int)> f
= ^(int x){NSLog(@"Hello function!! x=%d",x);};
f(1); // Hello function!! x=1
f(2); // Hello function!! x=2
std::function<T>は、Blocks対応に書き換え
てあるのかな??
39
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
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
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
Editor's Notes \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n