2013年9月16日 星期一

KVC機制

The NSKeyValueCoding informal protocol defines a mechanism by which you can access the properties of an object indirectly by name (or key), rather than directly through invocation of an accessor method or as instance variables. Thus, all of an object’s properties can be accessed in a consistent manner.
void changeName(Person *p, NSString *newName)
{
    // using the KVC accessor (getter) method
    NSString *originalName = [p valueForKey:@"name"];
    // using the KVC  accessor (setter) method.
    [p setValue:newName forKey:@"name"];
    NSLog(@"Changed %@'s name to: %@", originalName, newName);
}

沒有留言:

張貼留言