这个概念似乎给我带来了麻烦.为什么NSError对象需要将其指针传递给正在修改对象的方法?例如,不仅仅是传递对错误的引用做同样的事情吗?
NSError *anError;
[myObjc doStuff:withAnotherObj error:error];
Run Code Online (Sandbox Code Playgroud)
然后在doStuff中:
- (void)doStuff:(id)withAnotherObjc error:(NSError *)error
{
// something went bad!
[error doSomethingToTheObject];
}
Run Code Online (Sandbox Code Playgroud)
为什么上述工作不像大多数其他对象消息传递模式一样有效?为什么必须使用错误:(NSError**)错误?
我意识到这与现有帖子类似,(NSError**)错误是什么意思?,但我的问题有点不同.我理解双指针是如何工作的,以及这是常见的iOS API错误模式.我的问题更多的是关于单指针,以及为什么这段代码不起作用:
- (BOOL)someMethodWithError:(NSError *)error
{
...
if( errorOccured )
{
NSError *e = [[[NSError alloc] initWithDomain:@"" code:1 userInfo:nil] autorelease];
error = e;
return NO;
}
return YES;
}
Run Code Online (Sandbox Code Playgroud)
实施使用:
NSError *error = nil;
if( ![someObj someMethodWithError:error] )
{
NSLog(@"Error: %@", [error localizedDescription]);
}
Run Code Online (Sandbox Code Playgroud)
为什么方法实现中的赋值不会将指针重新分配给新的NSError对象?