0

最小化应用程序时,一切都很好,但是当我尝试部署繁荣时,此错误 * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[__NSArrayM insertObject:atIndex:]: object cannot be nil '

-(void) applicationDidEnterBackground:(UIApplication *)application
{
        NSDate *alertTime = [[NSDate date] dateByAddingTimeInterval:5];
        UIApplication* app = [[UIApplication sharedApplication] init];
        UILocalNotification* notifyAlarm = [[UILocalNotification alloc] init];
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:kAllNews]];

        NSError *error = nil;

        NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];

        NSError *jsonParsingError = nil;

        sortArray = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];

        if (notifyAlarm)
        {

        if ([[NSUserDefaults standardUserDefaults] objectForKey:@"news"])
        {
            newsNew = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"news"]];
        }

        if (newsNew.count > sortArray.count) {

        notifyAlarm.fireDate = alertTime;
        notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
        notifyAlarm.repeatInterval = 0;
        notifyAlarm.soundName = @"Glass.aiff";
        notifyAlarm.alertAction = @"Gipoteza";
        notifyAlarm.alertBody = @"Добавлена новая новость";
        [app scheduleLocalNotification:notifyAlarm];
    }
  }
}
4

1 回答 1

0

几个建议:

第一的; 您是否尝试将代码放入 applicationWillResignActive 中?

为什么在代码中引用 UIApplication?您已经将其作为方法的参数...

尝试在您的代码中放置断点以查看此错误发生的确切位置。我猜它发生在最后一行:[app scheduleLocalNotification:notifyAlarm],它发生是因为你的 notifyAlarm 对象由于某种原因是 nil。使用您的调试器单步执行代码以查看该对象是否确实已设置。

还; 请注意,您的代码从停用到暂停最多有 5 秒的时间。暂停时;没有代码可以运行。例如,如果您的 NSURLConnection 请求由于某种原因需要很长时间才能响应,您的应用程序将在代码完成之前暂停。

于 2013-08-24T09:54:58.887 回答