我已经构建并运行了一个iOS应用程序,它在启动时因AppDelegate.swift中的此错误而崩溃:
线程1:异常:“ ***-[__ NSArrayM insertObject:atIndex:]:对象不能为空
下面是代码的屏幕快照,仅供参考。
我无法识别该错误出现在代码的哪个部分,因此我可以通过应用断点来进一步调试它。 需要帮助解决此问题。
添加一段将对象插入数组以供参考的代码。
+ (NSArray *)relevantURLSchemes {
NSMutableArray *result = [[NSMutableArray alloc] init];
for (NSBundle *bundle in [[self class] relevantBundles]) {
NSArray *urlTypes = [bundle objectForInfoDictionaryKey:@"CFBundleURLTypes"];
for (NSDictionary *urlType in urlTypes) {
[result addObjectsFromArray:urlType[@"CFBundleURLSchemes"]];
}
}
return result;
}
答案 0 :(得分:0)
这是因为您试图在数组中插入一个空对象。
您需要先检查该值,然后才能将其插入数组。
@property (strong, nonatomic) NSMutableArray *arr;
使用前请确保已将其初始化。
_arr = [[NSMutableArray alloc] init];
在其中添加对象/值时,可以像这样检查。
NSString *strValue = nil;
if (strValue != nil) {
[self.arr addObject:strValue];
}
在插入数组时,请确保要插入索引,否则将在运行时使数组索引超出界限错误。
编辑
在将数据添加到数组之前,应检查是否为空。
id schemes = urlType[@"CFBundleURLSchemes"];
if (![schemes isEqual:[NSNull null]] && schemes != nil) {
[result addObjectsFromArray: schemes];
}