More Related Content Similar to 連邦の白いヤツ 「Objective-C」 Similar to 連邦の白いヤツ 「Objective-C」 (20) 連邦の白いヤツ 「Objective-C」18. "initialize aMyObject "
aMyObject initialize.
" 30 setSize aSquare "
aSquare setSize : 30.
" + 4 "
total := 3 + 4.
min: other
^self < other ifTrue: [self] ifFalse: [other].
23. #import <Foundation/Foundation.h>
//
@interface MyClass : NSObject {
int val;
}
- (id)init;
+ (void)classMethod:(id)arg; //
- (id)method:(NSObject*)arg1 with:(int)arg2;
- (id)method:(NSObject*)arg1 param:(int)arg2;
@end
//
@implementation MyClass
+ (void)classMethod:(id)arg {
// some operation
}
- (id)method:(NSObject*)arg1 with:(int)args2 {
return obj;
}
- (id)method:(NSObject*)arg1 param:(int)args2 {
return obj;
}
@end
24. #import <Foundation/Foundation.h>
//
@interface MyClass : NSObject {
int val;
}
- (id)init;
+ (void)classMethod:(id)arg; //
- (id)method:(NSObject*)arg1 with:(int)arg2;
- (id)method:(NSObject*)arg1 param:(int)arg2;
@end
//
@implementation MyClass
+ (void)classMethod:(id)arg {
// some operation
}
- (id)method:(NSObject*)arg1 with:(int)args2 {
return obj;
}
- (id)method:(NSObject*)arg1 param:(int)args2 {
return obj;
}
@end
26. // init
- (id)init {
self = [super init]; //
if(self != nil) {
val = 1;
obj = [[SomeClass alloc] init];
}
return self;
}
27. AlarmFake* f = [[AlarmFake alloc]init];
if( [f conformsToProtocol:@protocol(Alarm)] )
{
//
}
28. @interface ObjcClass : NSObject{
@private
int x_;
@protected //
int y_;
@public
int z_;
@package //
int w_;
}
@end
29. ObjcClass* oc = [[ObjcClass alloc]init];
int z = oc->z_; // public
int x = oc->x_; //
30. ObjcClass* oc = [[ObjcClass alloc]init];
int z = oc->z_; // public
int x = oc->x_; //
32. @interface ObjcClass : NSObject
- (void)print;
@end
@implementation ObjcClass
- (void)print {
NSLog(@"ObjcClass print:");
}
- (void)private_print {
NSLog(@"ObjcClass private_print:");
}
@end
void test() {
ObjcClass* oc = [[ObjcClass alloc]init];
[oc print]; // ObjcClass print:
[oc private_print]; // ObjcClass private_print:
33. @interface ObjcClass : NSObject
- (void)print;
@end
@implementation ObjcClass
- (void)print {
NSLog(@"ObjcClass print:");
}
- (void)private_print {
NSLog(@"ObjcClass private_print:");
}
@end
void test() {
ObjcClass* oc = [[ObjcClass alloc]init];
[oc print]; // ObjcClass print:
[oc private_print]; // ObjcClass private_print:
36. #include <boost/shared_ptr.hpp>
class CppClass{
public:
CppClass(){}
void print() const {
std::cout << "CppClass::print" << std::endl;
}
};
@interface ObjcClass : NSObject
- (void)print;
@end
@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
}
37. MyObject* obj = [[MyObject alloc] init];
[obj method :arg with:10];
[nil method :arg with:10]; //
38. MyObject* obj = [[MyObject alloc] init]; //
[obj retain]; //
NSLog(@"count=%d", [obj retainCount] ); // count=2
[obj release] ; //
[obj release] ; //
//
MyObject* obj = [[[MyObject alloc] init] autorelease];
[obj retain]; //
49. @interface NSObject <NSObject> {
Class
isa;
}
- (id)init;
+ (id)alloc;
- (void)dealloc;
- (id)copy;
+ (Class)superclass;
+ (Class)class;
- (IMP)methodForSelector:(SEL)aSelector;
+ (NSString *)description;
+ (BOOL)isSubclassOfClass:(Class)aClass
@end
50. - (BOOL)isEqual:(id)object;
- (NSUInteger)hash;
- (Class)superclass;
- (Class)class;
- (id)self;
- (BOOL)isKindOfClass:(Class)aClass;
- (BOOL)isMemberOfClass:(Class)aClass;
- (BOOL)conformsToProtocol:(Protocol *)aProtocol;
- (BOOL)respondsToSelector:(SEL)aSelector;
- (id)retain;
- (oneway void)release;
- (id)autorelease;
- (NSUInteger)retainCount;
52. Class meta = [MySubClass class]; // MySubClass
Class metameta = [meta class];
if( [meta isKindOfClass:[NSObject class]] )
{
NSLog(@"YES");
}
53. NSBundle* mainBundle = [NSBundle mainBundle];
NSBundle* moduleBundle = [mainBundle bundleWithPath : @"path"];
NSString* loadbleClassName =
Class c = [moduleBundle classNamed : loadbleClassName ];
id = [[c alloc] init] autorelease];
56. @interface MySubClass : MyObject {
int x_;
}
@property int x_;
@end
@implementation MySubClass
-(int)x_{ return x_;} // getter
-(void)setX_:(int)x { x_ = x;} // setter
@end
MySubClass* obj = [[MySubClass alloc] init];
int xx= p.x_;
p.x_ = 200;
57. @interface MySubClass : MyObject {
int x_;
}
@property int x_;
@end
@implementation MySubClass
-(int)x_{ return x_;} // getter
-(void)setX_:(int)x { x_ = x;} // setter
@end
MySubClass* obj = [[MySubClass alloc] init];
int xx= p.x_;
p.x_ = 200;
58. @interface MySubClass : MyObject {
int y_;
}
@property int y_;
@end
@implementation MySubClass
@synthesize y_;
@end
int yy= p.y_;
p.y_ = 200;
59. @interface MySubClass : MyObject {
int y_;
}
@property int y_;
@end
@implementation MySubClass
@synthesize y_;
@end
int yy= p.y_;
p.y_ = 200;
66. AlarmFake* f = [[AlarmFake alloc]init];
id<Alarm> f2 = f;
if( [f2 conformsToProtocol:@protocol(Alarm)] )
{
//
}
67. AlarmFake* f = [[AlarmFake alloc]init];
id<Alarm> f2 = f;
if( [f2 conformsToProtocol:@protocol(Alarm)] )
{
//
}
69. @protocol Alarm
-(void)nowTime;
@optional
-(void)snooze;
@end
@interface MyClock : NSObject<Alarm>
-(void)nowTime;
//-(void)snooze;
@end
70. @protocol Alarm
-(void)nowTime;
@optional
-(void)snooze;
@end
//-(void)snooze
@interface MyClock : NSObject<Alarm>
-(void)nowTime;
@end
@implementation MyClock
-(void)nowTime { NSLog(@"nowTime"); }
@end
MyClock* a = [[MyClock alloc]init];
[a nowTime];
[a snooze]; //
71. @protocol Alarm
-(void)nowTime;
@optional
-(void)snooze;
@end
//-(void)snooze
@interface MyClock : NSObject<Alarm>
-(void)nowTime;
@end
@implementation MyClock
-(void)nowTime { NSLog(@"nowTime"); }
@end
MyClock* a = [[MyClock alloc]init];
[a nowTime];
[a snooze]; //
75. @interface MyObject : NSObject {
int val;
}
- (void)bar;
@end
@implementation MyObject
@end
@interface MyObject(EventHandler)
- (void)buttonClicked;
@end
@implementation MyObject(EventHandler)
- (void)buttonClicked{
NSLog(@"calll buttonClicked");
}
@end
void category_test(){
MyObject* obj = [[MyObject alloc]init];
[obj buttonClicked]; // call buttonClicked
}
90. @try{
if( ){
//
errorcode = 1000;
[NSException raise : @"Fatal Error occured"
format : @"%d", errorcode];
//
@throw [[MyException alloc]init];
}
}
@catch( NSException* ex )
{
NSLog(@"name=%@, reason=%@",[ex name], [ex reason]);
[ex raise]; //
@throw; //
}
@catch( ... )
{
NSLog(@"unknown exception");
}
@finaly{
}
91. @try{
if( ){
//
errorcode = 1000;
[NSException raise : @"Fatal Error occured"
format : @"%d", errorcode];
//
@throw [[MyException alloc]init];
}
}
@catch( NSException* ex )
{
NSLog(@"name=%@, reason=%@",[ex name], [ex reason]);
[ex raise]; //
@throw; //
}
@catch( ... )
{
NSLog(@"unknown exception");
}
@finaly{
}
100. void blocks_caller( void (^f)(void) )
{
f();
}
void blocks_test()
{
blocks_caller( ^(){ NSLog(@"test1");} );
blocks_caller( ^(){ NSLog(@"test2");} );
}
101. Func<int, Func<int, int>> f = x => y => x + y;
Func<int, int> fc = f(1);
int x = fc(3); // 1 + 3
function< function<int(int)> (int) > f =
[] (int x){ return [x](int y){ return x+y;}; };
auto fc = f(1);
int x = fc(3); // 1 + 3
102. 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));
Block_release(fc);
}