0

私のコードは次の例外をスローします:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]' 

行上:

[[UIApplication sharedApplication] cancelLocalNotification:appDelegate.dailyNotificationMonday];

コード ブロック内:

if ([[defaults objectForKey:@"dailyReminder"] isEqualToString:@"1"]){
        //Schedule Notifications for each checked day
        if ([[defaults objectForKey:@"dailyReminderMonday"] isEqualToString:@"1"])
        {
            //Set up the local notification
            appDelegate.dailyNotificationMonday = [[UILocalNotification alloc] init];
            if(appDelegate.dailyNotificationMonday){
                //Set fire date to alert time
                appDelegate.dailyNotificationMonday.fireDate = mondayFireTime;
                //Set Alert body
                appDelegate.dailyNotificationMonday.alertBody = textField.text;
                //Set time zone to default
                appDelegate.dailyNotificationMonday.timeZone = [NSTimeZone defaultTimeZone];
                //Repeat the notification everyday (fires at same time
                //as initial notification)
                appDelegate.dailyNotificationMonday.repeatInterval = NSWeekCalendarUnit;

                // schedule notification
                UIApplication *app = [UIApplication sharedApplication];
                [app scheduleLocalNotification:appDelegate.dailyNotificationMonday];

                NSLog(@"Monday Fire Date: %@", appDelegate.dailyNotificationMonday.fireDate);
            }
        }
        else {
           [[UIApplication sharedApplication] cancelLocalNotification:appDelegate.dailyNotificationMonday]; 
        }

        if ([[defaults objectForKey:@"dailyReminderTuesday"] isEqualToString:@"1"])
        {
            //Set up the local notification
            appDelegate.dailyNotificationTuesday = [[UILocalNotification alloc] init];
            if(appDelegate.dailyNotificationTuesday){
                //Set fire date to alert time
                appDelegate.dailyNotificationTuesday.fireDate = tuesdayFireTime;
                //Set Alert body
                appDelegate.dailyNotificationTuesday.alertBody = textField.text;
                //Set time zone to default
                appDelegate.dailyNotificationTuesday.timeZone = [NSTimeZone defaultTimeZone];
                //Repeat the notification everyday (fires at same time
                //as initial notification)
                appDelegate.dailyNotificationTuesday.repeatInterval = NSWeekCalendarUnit;

                // schedule notification
                UIApplication *app = [UIApplication sharedApplication];
                [app scheduleLocalNotification:appDelegate.dailyNotificationTuesday];

                NSLog(@"Tuesday Fire Date: %@", appDelegate.dailyNotificationTuesday.fireDate);
            }
        }
        else {
            [[UIApplication sharedApplication] cancelLocalNotification:appDelegate.dailyNotificationTuesday]; 
        }

        if ([[defaults objectForKey:@"dailyReminderWednesday"] isEqualToString:@"1"])
        {
            //Set up the local notification
            appDelegate.dailyNotificationWednesday = [[UILocalNotification alloc] init];
            if(appDelegate.dailyNotificationWednesday){
                //Set fire date to alert time
                appDelegate.dailyNotificationWednesday.fireDate = wednesdayFireTime;
                //Set Alert body
                appDelegate.dailyNotificationWednesday.alertBody = textField.text;
                //Set time zone to default
                appDelegate.dailyNotificationWednesday.timeZone = [NSTimeZone defaultTimeZone];
                //Repeat the notification everyday (fires at same time
                //as initial notification)
                appDelegate.dailyNotificationWednesday.repeatInterval = NSWeekCalendarUnit;

                // schedule notification
                UIApplication *app = [UIApplication sharedApplication];
                [app scheduleLocalNotification:appDelegate.dailyNotificationWednesday];

                NSLog(@"Wednesday Fire Date: %@", appDelegate.dailyNotificationWednesday.fireDate);
            }
        }
        else {
            [[UIApplication sharedApplication] cancelLocalNotification:appDelegate.dailyNotificationWednesday]; 
        }

        if ([[defaults objectForKey:@"dailyReminderThursday"] isEqualToString:@"1"])
        {
            //Set up the local notification
            appDelegate.dailyNotificationThursday = [[UILocalNotification alloc] init];
            if(appDelegate.dailyNotificationThursday){
                //Set fire date to alert time
                appDelegate.dailyNotificationThursday.fireDate = thursdayFireTime;
                //Set Alert body
                appDelegate.dailyNotificationThursday.alertBody = textField.text;
                //Set time zone to default
                appDelegate.dailyNotificationThursday.timeZone = [NSTimeZone defaultTimeZone];
                //Repeat the notification everyday (fires at same time
                //as initial notification)
                appDelegate.dailyNotificationThursday.repeatInterval = NSWeekCalendarUnit;

                // schedule notification
                UIApplication *app = [UIApplication sharedApplication];
                [app scheduleLocalNotification:appDelegate.dailyNotificationThursday];

                NSLog(@"Thursday Fire Date: %@", appDelegate.dailyNotificationThursday.fireDate);
            }
        }
        else {
            [[UIApplication sharedApplication] cancelLocalNotification:appDelegate.dailyNotificationThursday]; 
        }

        if ([[defaults objectForKey:@"dailyReminderFriday"] isEqualToString:@"1"])
        {
            //Set up the local notification
            appDelegate.dailyNotificationFriday = [[UILocalNotification alloc] init];
            if(appDelegate.dailyNotificationFriday){
                //Set fire date to alert time
                appDelegate.dailyNotificationFriday.fireDate = fridayFireTime;
                //Set Alert body
                appDelegate.dailyNotificationFriday.alertBody = textField.text;
                //Set time zone to default
                appDelegate.dailyNotificationFriday.timeZone = [NSTimeZone defaultTimeZone];
                //Repeat the notification everyday (fires at same time
                //as initial notification)
                appDelegate.dailyNotificationFriday.repeatInterval = NSWeekCalendarUnit;

                // schedule notification
                UIApplication *app = [UIApplication sharedApplication];
                [app scheduleLocalNotification:appDelegate.dailyNotificationFriday];

                NSLog(@"Friday Fire Date: %@", appDelegate.dailyNotificationFriday.fireDate);
            }
        }
        else {
            [[UIApplication sharedApplication] cancelLocalNotification:appDelegate.dailyNotificationFriday]; 
        }

        if ([[defaults objectForKey:@"dailyReminderSaturday"] isEqualToString:@"1"])
        {
            //Set up the local notification
            appDelegate.dailyNotificationSaturday = [[UILocalNotification alloc] init];
            if(appDelegate.dailyNotificationSaturday){
                //Set fire date to alert time
                appDelegate.dailyNotificationSaturday.fireDate = saturdayFireTime;
                //Set Alert body
                appDelegate.dailyNotificationSaturday.alertBody = textField.text;
                //Set time zone to default
                appDelegate.dailyNotificationSaturday.timeZone = [NSTimeZone defaultTimeZone];
                //Repeat the notification everyday (fires at same time
                //as initial notification)
                appDelegate.dailyNotificationSaturday.repeatInterval = NSWeekCalendarUnit;

                // schedule notification
                UIApplication *app = [UIApplication sharedApplication];
                [app scheduleLocalNotification:appDelegate.dailyNotificationSaturday];

                NSLog(@"Saturday Fire Date: %@", appDelegate.dailyNotificationSaturday.fireDate);
            }
        }
        else {
            [[UIApplication sharedApplication] cancelLocalNotification:appDelegate.dailyNotificationSaturday]; 
        }

        if ([[defaults objectForKey:@"dailyReminderSunday"] isEqualToString:@"1"])
        {
            //Set up the local notification
            appDelegate.dailyNotificationSunday = [[UILocalNotification alloc] init];
            if(appDelegate.dailyNotificationSunday){
                //Set fire date to alert time
                appDelegate.dailyNotificationSunday.fireDate = sundayFireTime;
                //Set Alert body
                appDelegate.dailyNotificationSunday.alertBody = textField.text;
                //Set time zone to default
                appDelegate.dailyNotificationSunday.timeZone = [NSTimeZone defaultTimeZone];
                //Repeat the notification everyday (fires at same time
                //as initial notification)
                appDelegate.dailyNotificationSunday.repeatInterval = NSWeekCalendarUnit;

                // schedule notification
                UIApplication *app = [UIApplication sharedApplication];
                [app scheduleLocalNotification:appDelegate.dailyNotificationSunday];

                NSLog(@"Sunday Fire Date: %@", appDelegate.dailyNotificationSunday.fireDate);
            }
        }
        else {
            [[UIApplication sharedApplication] cancelLocalNotification:appDelegate.dailyNotificationSunday]; 
        }


    }
    else {
        //Remove all daily notifications
        [[UIApplication sharedApplication] cancelLocalNotification:appDelegate.dailyNotificationMonday];
        [[UIApplication sharedApplication] cancelLocalNotification:appDelegate.dailyNotificationTuesday];
        [[UIApplication sharedApplication] cancelLocalNotification:appDelegate.dailyNotificationWednesday];
        [[UIApplication sharedApplication] cancelLocalNotification:appDelegate.dailyNotificationThursday];
        [[UIApplication sharedApplication] cancelLocalNotification:appDelegate.dailyNotificationFriday];
        [[UIApplication sharedApplication] cancelLocalNotification:appDelegate.dailyNotificationSaturday];
        [[UIApplication sharedApplication] cancelLocalNotification:appDelegate.dailyNotificationSunday];
    }

AppDelegate には次のコードもあります。

//Initialze daily notifications
        dailyNotificationMonday = [[UILocalNotification alloc] init];
        dailyNotificationTuesday = [[UILocalNotification alloc] init];
        dailyNotificationWednesday = [[UILocalNotification alloc] init];
        dailyNotificationThursday = [[UILocalNotification alloc] init];
        dailyNotificationFriday = [[UILocalNotification alloc] init];
        dailyNotificationSaturday = [[UILocalNotification alloc] init];
        dailyNotificationSunday = [[UILocalNotification alloc] init];

        //Initialize submition notifications
        submitNotification = [[UILocalNotification alloc] init];
        badgerNotification = [[UILocalNotification alloc] init];

        //Initialize Automatic backup notification
        automaticBackupNotification  = [[UILocalNotification alloc] init];

アプリケーションが最初に起動されたときに一度だけ呼び出されます。

ローカル通知の使用にまったく慣れていませんが、ここで何が起こっているのかわかりません。存在しない通知をキャンセルしようとしているように思えます。

アドバイスをいただければ幸いです。

ありがとう、

タイシン

4

2 に答える 2

1

のようappDelegate.dailyNotificationMondayですnil。これは に渡す有効な値ではないようです-cancelLocalNotification:

やってみました:

if (appDelegate.dailyNotificationMonday)
    [[UIApplication sharedApplication] cancelLocalNotification:appDelegate.dailyNotificationMonday];

それが役立つことを願っています。

于 2012-07-31T16:13:59.060 に答える
0

appDelegate.dailyNotificationMonday は、この if ブロックでのみスケジュールするため、nil です。

if ([[defaults objectForKey:@"dailyReminderMonday"] isEqualToString:@"1"])

そうではなく、else ブロックで cancelLocalNotification が呼び出された場合は、基本的に、まだスケジュールされていない通知をキャンセルするようにアプリに指示していることになります。実はelseブロックは必要ないと思いますが、それを取り除いてみましたか?

于 2012-07-31T16:20:16.733 に答える