- pointers are generally objects. NSArray* array –> then array is an object. call object’s method by square brackets: [array size]
- id type means “Object” in java. id a = @”string” means Object a = “string” in java.
- nil is similar to null in java. however, it is safe to call [nil someMethod], it will not crash, but it will return null. may result in strange behavior.
- NSString can be initialized using @ character, and it is in Unicode.
NSString* s = @"Some string"
- NSArray can be initialized using @ too: NSArray* str_arr = @[@”a”, @”b”, @”c”];
- selector is something like the “function pointers” in C. to refer a selector, use
@selector(method_name:)
- #import is a better #include.
- for methods, + means “static” and – is “instance”.
- Method declaration:
+ (UIColor*) colorWithRed: (CGFloat) red green: (CGFloat) green blue: (CGFloat) blue alpha: (CGFloat) alpha
colorWithRed:green:blue:alpha: is the method name.
The (CGFloat) is param type, and the subsequenct red, green, blue and alpha are actually params. - No method overloading is supported. (methods cannot have some name)
- CFTypeRefs can sometime be used to convert NS data type (objc) to CF data type (C). e.g., NSString* x; (CFStringRef)x will give CFString (CF= Core Foundation, NS=next step)
- Closure. (called Block) in objc. in the form of ^(id obj1, BOOL f){ … };
NSArray* arr2 = [arr sortedArrayUsingComparator: ^(id obj1, id obj2) {}
a __block qualifier will make a local variable accessible with a block. (like final for anonymous inner class in java)
- class structure: usually inherit from NSObject – even for allocation.
- class structure: @interface to declare the interface of the class (method prototype in C) and @implementation to write the real implementation.
example: -
@interface MyClass : NSObject - (NSString*) sayGoodnightGracie; @end @implementation MyClass { // instance variable declarations go here } - (NSString*) sayGoodnightGracie { return @"Good night, Gracie!"; } @end
- Alloc-init: alloc is to alloc memory, init is something similar to a constructor. it must be called throughout the construction chain (designated initializer(default constructor?)). Sometime the new method in NSObject is equal to [[Object alloc] init]
- self = this; super = super
1 comment
Join the conversationLiang Qi - 04/15/2014
3. [nil someMethod] not crash, it should be a feature of the language.