为什么我的 iOS 应用程序在启动时崩溃

why my iOS app is getting crashed on launching

我已经构建了 运行 一个 iOS 应用程序,它一启动就崩溃了,并在 AppDelegate.swift:

中出现了这个错误

Thread 1: Exception: "*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil

以下为代码截图,供参考

我无法确定错误出现在代码的哪一部分,因此我无法通过应用断点进一步调试它。 需要帮助解决这个问题。

添加将对象插入数组以供参考的代码段。

+ (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;
}

这是因为您试图在数组中插入空对象。

您需要先检查该值才能将其插入数组。

@property (strong, nonatomic) NSMutableArray *arr;

请确保在使用前已对其进行初始化。

_arr = [[NSMutableArray alloc] init];

当您在其中添加 object/value 时,您可以这样检查。

NSString *strValue = nil;
if (strValue != nil) {
    [self.arr addObject:strValue];
}

当您在数组中插入时,请确保插入索引,否则您将在 运行 时出现数组索引超出范围的错误。

编辑

在将数据添加到数组之前,您应该检查是否为空值。

id schemes = urlType[@"CFBundleURLSchemes"];
if (![schemes isEqual:[NSNull null]] && schemes != nil) {
   [result addObjectsFromArray: schemes];
 }