gpt4 book ai didi

ios - 超出范围后 NSError 为 nil

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:39:55 27 4
gpt4 key购买 nike

我的类(class)有成员 __strong NSError *authError;我想用一种方法捕获并创建它,然后 checkin 另一种方法。所以如果方法一出现错误:

if([returnString rangeOfString:@"dd"].location != NSNotFound) {  
NSMutableDictionary *errorDetail = [NSMutableDictionary dictionary];
[errorDetail setValue:@"Wrong user crediantials" forKey:NSLocalizedDescriptionKey];
authError = [NSError errorWithDomain:@"mydomain" code:101 userInfo:errorDetail];

return nil;
}

我直接赋给了authError类成员。但是在最后一步之后 breket authError 变成了 nil。在方法 2 中:

if(authError != nil){
return NO;
}

条件返回是

一些附加信息:方法 2 是 shouldPerformSegueWithIdentifier有人可以给我提示吗?

最佳答案

这里的问题与“之后”这个词的定义有关。您在方法 1 中设置实例变量并在方法 2 中获取它。但这并没有告诉您真正的操作顺序。如果碰巧在方法 1 有机会运行之前调用了方法 2,那么方法 2 会发现您的实例变量为 nil(因为那是它的自动初始化值)。

我的书有一个 riff on this very topic (“入口点”是指运行时调用的方法):

Adding to your challenges is the fact that you can’t really know precisely when an entry point will be called. The documentation may give you a general sense, but in most cases it doesn’t guarantee anything about precisely when events will arrive and in what order. What you think is going to happen, and even what the documentation leads you to believe is going to happen, might not be quite what really does happen. Your own code can trigger unintended events. The documentation might not make it clear just when a notification will be sent. There could even be a bug in Cocoa such that events are called in a way that seems to contradict the documentation. And you have no access to the Cocoa source code, so you can’t work out the underlying details. Therefore I also recommend that as you develop your app, you instrument your code heavily with caveman debugging (NSLog; see Chapter 9). As you test your code, keep an eye on the console output and check whether the messages make sense. You may be surprised at what you discover.

关于ios - 超出范围后 NSError 为 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22287424/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com