除左之前係針對UI elements 去學
仲有啲特別野係可以其他地方都用到
[self setValue:someValues forKey:keyNameOrVariable];
呢個message係一啲regular 既variable name好有用
例如你有五個NSArray
NSArray arr1 =...
NSArray arr2 =...
NSArray arr3 =...
NSArray arr4 =...
NSArray arr5 =...
而每個NSArray 都要init 同比啲elements
for (int i = 1 ; i <=5 ; i++)
{
initialize..
initialize needed objects..
NSArray *tempArray = [[NSArray alloc] initWithObjects:obj1, obj2, obj3 , .. nil];f
NSString *arrayName = [[NSSting alloc] initWithFormat:@"arr%d" , i];
[self setValue:tempArray forKey:arrayName];
}
咁就set 曬全部array
P.S. From Beginning iOS 5 Development Apress Chapter 10
Why are we
using KVC here instead of just setting the delegate directly? One of the
benefits of KVC is that it frees us from knowing the specifics of other
classes' interfaces, which leads to less tightly coupled code. If we
wanted to call the method directly, we would need to declare an
interface of some kind that included the setDelegate:
method, and cast our destination variable to be a type that implemented
that method. Using KVC, our code doesn't need to know anything about the
setDelegate: method (apart from the fact that the receiver responds to it), so we don't need to have any declared interface to it. BIDTaskListController doesn't import BIDTaskDetailController's
header, and, in fact, doesn't even know that it exists, and that's a
good thing! In general, the fewer dependencies you have between classes,
the better. Properly applied, KVC can help you move your code in that
direction.
沒有留言:
張貼留言