博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Object-C Foundamental Knowledge

Posted on 2011-11-25 23:05  sytjs  阅读(326)  评论(0)    收藏  举报

语言的构架和设计一定有其逻辑,不可能杂乱无章或随心所欲!

1. 类属性修饰符:assign, retain, copy, nonatomic, strong, weak

  1.1. assign: In your setter method for the property, there is a simple assignment of your instance variable to the new value, eg:

- (void)setString:(NSString*)newString
{
    string = newString;
}

This can cause problems since Objective-C objects use reference counting, and therefore by not retaining the object, there is a chance that the string could be deallocated whilst you are still using it.Use cases:- Scalar types- primitive types- objects do not directly own

1.2. retain:  Offical Doc. : Retaining an object creates a strong reference, and an object cannot be deallocated until all of its strong references are released. If two objects retain each other, neither object ever gets deallocated because the connection between them cannot be broken.this retains the new value in your setter method. For example:

- (void)setString:(NSString*)newString
{
    [newString retain];  // Setup the reference to newString;
    [string release];  // release count to the prevalue of string
    string = newString;
}

This is safer, since you explicitly state that you want to maintain a reference of the object, and you must release it before it will be deallocated.ci

  + Interest case: retain  circle(cycle)

    - A creates B A sets itself as B's delegate … A is released by its owner; If B had retained A, A wouldn't be released, as B owns A, thus A's dealloc would never get called, causing both A and B to leak. (the parent retains the child and the child retains the parent so neither is ever released).)

   - weakmodifier serves best at this circumstances

  + strong is a replacement of retain in a auto-reference-counting(ARC) system

  + You either have ARC on or off for a particular file. If its on you cannot use retain, release, autorelease, etc... Instead you use strong|weak for properties or __strong|__weak(precompile instruction) for local variables (defaults to __strong). Strong is the equivalent to retain, however ARC will manage the release for you.

    - Config ARC on|off : Build Phases--->Apple LLVM compiler 3.0- Language--->Objective-C Automatic Reference Counting YES<->NO

1.3.  copy: this makes a copy of the string in your setter method:

- (void)setString:(NSString*)newString
{
    if(string!=newString)
    {
        [string release];
        string = [newString copy];
    }
}

This is often used with strings, since making a copy of the original object ensures that it is not changed whilst you are using it.

1.4. nonatomic, atomic 

The code for a non atomic retain getter and setter conceptually looks something like:

-(id) foo
{
    return fooIvar;
}

-(void) setFoo: (id) newFoo
{
    [newFoo retain];
    [fooIvar release];
    fooIvar = newFoo;
}

The code for an atomic getter and setter conceptually looks something like this:

-(id) foo
{
    @synchronized(self)
    {
        return [[fooIvar retain] autorelease]; // if other thread call set on the same time, the value is still in auto release pool
    }
}

-(void) setFoo: (id) newFoo
{
    @synchronized(self)
    {
        [newFoo retain];
        [fooIvar release];
        fooIvar = newFoo;
    }
}

The implementation details are different, notably the locking ismore light weight than synchronising the object with the ivar.

In the non atomic case and in a multithreaded environment, you can't guarantee that the getter will give you a valid object because between the getter returning the reference and the caller retaining it (or doing anything else) another thread could call the setter, releasing the object and possibly deallocating it.

In the atomic case, this can't happen because the getter puts the object in the thread's autorelease pool before returning it. If another thread calls the setter and releases the object before the caller has a chance to retain it, it doesn't matter because of the ownership the autorelease pool has.

1.5. other key words

Release: Decrease retain count by 1 ; the dealloc method will get called when the retain count reaches 0.

Autorelease: Deleayed release, will decrease retain count by 1 when the autoreleasepool is drained, which is most likely in the next run but not guaranteed.

Drain: To drain the autorelease pool

Nil: Null for objective C objects

Null: Null for C pointers.

The difference is that while NULL represents zero for any pointer, nil is specific to objects (e.g., id) and Nil is specific to class pointers.

 

2. Instance variable & property

/ === In * .h ===

@interface MyObject {
    NSString *propertyName;

}

//…

@property (nonatomic, retain) NSString *propertyName;

@end

// === In *.m @implementation ===

//…

@synthesize propertyName /* = otherVarName */;

The @synthesize line tells the compiler to generate these getter/setters for you, using the member(instance) variable with the same name of the property to store the value (or otherVarName if you use the syntax in comments).