为什么要改变-testMethod
方法中的自我对象?这是非常非法的。
你实际上在做的是设置一个局部变量self
,它作为参数传递给你的方法,并设置为一个新的值。这意味着你没有编辑方法的接收器,你只是编辑你的参数。
当你的方法在运行时调用C函数objc_msgSend()
叫做:
// Declaration of objc_msgSend
id objc_msgSend(id receiver, SEL selector, ...);
现在,当你打电话给你的方法...
[myInst testMethod];
...其实这是被称为什么在运行时:
objc_msgSend(myInst, @selector(testMethod));
你已经看到发生了什么?在你的方法实现中,self
变量被设置为objc_msgSend
的第一个参数。当你重新分配self
时,你的不是编辑变量myInst
包含的内容,因此你是而不是编辑你通过的原始实例。您只需将myInst
,即self
(本地变量)设置为您已知的指针。函数的调用者不会注意到这个变化。
比较你的代码下面的C代码:
void myFunction(int a) {
a = 3;
}
int b = 2;
myFunction(b);
printf("%d\n", b);
// The variable b still has the original value assigned to it
上面的代码做同样的事你做:
// Variation on objc_msgSend
void myMethodWrittenInC(id myInst) {
// Local variable changes, but will not change in the calling code
myInst = nil;
}
MyClass *myObj;
myObj = [[MyClass alloc] init];
myMethodWrittinInC(myObj);
// At this point myObj is not nil
最后,这是你要做的:
- (void)testMethod
{
NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"myEncodedObjectKey"];
// You assign the local variable self (passed as an invisible argument
// to your method) to your new instance, but you do not edit the original
// instance self pointed to. The variable currentProfile does not change.
self = (Profile *) [NSKeyedUnarchiver unarchiveObjectWithData:data];
for (int i = 0; i < self.avatar.count; i++)
[self.avatar
replaceObjectAtIndex:i
withObject:[UIImage imageWithData:[self.avatar objectAtIndex:i]]];
if ([[self.avatar objectAtIndex:0] isKindOfClass:[UIImage class]])
NSLog(@"UIImage");//at this moment it's UIImage
}
// (1) Here currentProfile points to an instance of your class
[currentProfile testMethod];
// (2) it calls the method, but the local variable does not change
// and still points to the same instance.
if ([[currentProfile.avatar objectAtIndex:0] isKindOfClass:[NSData class]])
NSLog(@"NSData");//Moment later it is NSData
你真是太棒了!非常感谢,哥们! – 2012-01-13 08:16:15
这是写在您的个人资料中的内容:“如果您有任何有关编程或请求的问题,请不要犹豫与我联系”。我总是有很多关于编码的问题,比如现在的问题,所以如果你不是在开玩笑,我会打扰你一下。 – 2012-01-13 08:21:52
没问题。我总是乐于提供帮助 – v1Axvw 2012-01-13 16:57:56