Date post: | 15-Apr-2017 |
Category: |
Software |
Upload: | futada-takashi |
View: | 293 times |
Download: | 0 times |
Objective CPart 1 Language
Overview
NeXTSTEP
Smalltalk
https://www.youtube.com/watch?v=j02b8Fuz73A
Language Design
Static and Strongly Typed
Keyword
No GC (ARC)
Superset of C (works as C compiler)
Not method but message
Protocol & Delegate Category
Hello World
NSLog(@“Hello World”);
Interpolation %@
Types
C types
int, char, float, const, unsigned
Objective C
NSObject, NSString, NSNumber, NSArray, NSDictionary
NSMutableArray
id
Methods [Messages]
obj.method(param);
[objmessage:param];
[objsayHello:@“Hello!”];
Methods [Messages]
obj.method(param1, param2);
[obj message:param1, keyword2:param2]
[person setFirst:@“John” setLast:@“Doe” setAge:@34]
The message signature is setFirst:setLast:setAge:
- (void) sayHello: (NSString *) param {
NSLog(@“Hello %@ World”, param);
};
- (String) sayHello:(Type)param1
keyword2:(Type)param2
keyword3:(Type)param3 {};
Methods [Messages]@Implementation
List and Dictionary
NSArray*list=@[obj1,obj2,obj3,…];
NSDictionary*dict=@{key:val,key:val}NSDictionary*dict=@{key:val,key:val}
^Callback (Block)
^ refers to an anonymous function
Define a block called Callback, which takes a NSString, then return void
Pass in a block
Implement a method called ajax:word:ajax takes a Callback type
ClassPerson.h
Person.m
Caller.m
#import “Person.h”
Person *p = [Person new];
@interface
@end
@implementation
@end
- (void) sayHello {…};
- (void) sayHello;
[p sayHello];
visible
implement
Class
Person.h
Person.m
Caller.m
Selector
Reflection in Java
SEL method = NSSelectorFromString(@“getFullname”);
[obj performSelector:method];
<Protocol>Interface in Java
C21Protocol.h
Delegate Pattern
Like Java, Protocol is used in Delegate Pattern. But, use of Block is recommended.
-doYouKnowObjectiveC
<Protocol>
Developer.m
Manager.mClient
Delegate patterns are used in Views and Controllers. I’ll show you the code in the GUI part.
- delegate
-doYouKnowObjectiveC
Category
Extends the existing class (like Ruby’s monkey patch)
Person+age.h
Person+age.mCaller.m
To extend Person, prepare two files called:
Person+age.hPerson+age.m
Add getAge()
Class Extension
Class extension is like anonymous Category ()
Define @interface in a .m file
The methods are private scope.
They are not visible from outside of the file.
Property Option (avoid memory leak)
Button
View Controller
(weak) btnStrongweak
View points to the Button object strongly,
while Controller.btn points to the Button weekly.
Thus, Compiler (ARC) can delete the Button object when the Strong link is detached.There is no GC in Objective-C. Compiler will insert destructor statements.