gpt4 book ai didi

ios - Xcode,无法识别的选择器发送到实例 - 但在哪一行?

转载 作者:行者123 更新时间:2023-11-29 10:39:56 24 4
gpt4 key购买 nike

我在处理 JSON 时遇到了常见错误,

-[NSNull length]: unrecognized selector sent to instance 0x102aa8b50

我完全理解这一点,知道如何处理它等等(顺便说一句,这是一个聪明的方法 https://stackoverflow.com/a/16610117/294884 )

我的问题是,在 Xcode 中,我如何判断是哪个 [length] 调用导致了问题?

Xcode 似乎只是显示它发生在“main 的第 7 行”-!

我不是 Xcode 调试专家,怎么办?

enter image description here

顺便说一句,关于具体的示例问题(NSNull 问题),我提请您注意 JR 下面提到的令人惊叹的库:github.com/jrturton/NSJSONSerialization-NSNullRemoval

最佳答案

在 Xcode 的断点 Pane 中添加一个“异常断点”。

Add Exception Breakpoint

大多数情况下,添加此代码后,代码引发的所有异常都会在正确的行上突出显示。

您正在获取 NSNull 对象,因为 JSON 包含如下内容:

{
"someKey": null
}

然后,当您使用 [json objectForKey:@"someKey"] 获取值时,它将返回一个 NSNull 对象,而不是 nil。因为 nil 意味着 kay 不存在,这是它的。

您可以使用简单的 Objective-C 类别轻松解决此问题:

NSDictionary+NotNull.h

#import <Foundation/Foundation.h>

/*! This category extends NSDictionary to work around an issue with NSNull object.
*/
@interface NSDictionary (NotNull)

/*! @abstract Returns the value associated with a given key, but only if the value is not NSNull.
@param aKey The key for which to return the corresponding value.
@return The value associated with the given key, or nil if no value is associated with the key, or the value is NSNull.
*/
- (id)objectOrNilForKey:(id)aKey;

NSDictionary+NotNull.m

#import "NSDictionary+NotNull.h"

@implementation NSDictionary (NotNull)

- (id)objectOrNilForKey:(id)aKey
{
id object = [self objectForKey:aKey];
if (object == [NSNull null]) {
return nil;
}
return object;
}

@end

关于ios - Xcode,无法识别的选择器发送到实例 - 但在哪一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25061654/

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