Objective-C super simple notes

  1. pointers are generally objects. NSArray* array –> then array is an object. call object’s method by square brackets: [array size]
  2. id type means “Object” in java. id a = @”string” means Object a = “string” in java.
  3. 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.
  4. NSString can be initialized using @ character, and it is in Unicode. NSString* s = @"Some string"
  5. NSArray can be initialized using @ too: NSArray* str_arr = @[@”a”, @”b”, @”c”];
  6. selector is something like the “function pointers” in C. to refer a selector, use @selector(method_name:)
  7. #import is a better #include.
  8. for methods, + means “static” and – is “instance”.
  9. 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.

  10. No method overloading is supported. (methods cannot have some name)
  11. 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)
  12. 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)

  13. class structure: usually inherit from NSObject – even for allocation.
  14. class structure: @interface to declare the interface of the class (method prototype in C) and @implementation to write the real implementation.
    example:
  15. @interface MyClass : NSObject
        - (NSString*) sayGoodnightGracie;
    @end
    @implementation MyClass {
            // instance variable declarations go here
    }
    - (NSString*) sayGoodnightGracie {
            return @"Good night, Gracie!";
    } 
    @end
  16. 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]
  17. self = this; super = super