-[NSDictionary initWithObjects:forKeys:]:对象数(0)与键数(2)不同 [英] -[NSDictionary initWithObjects:forKeys:]: count of objects (0) differs from count of keys (2)

查看:372
本文介绍了-[NSDictionary initWithObjects:forKeys:]:对象数(0)与键数(2)不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到

-[NSDictionary initWithObjects:forKeys:]:对象(0)的数量与键(2)的数量不同'

-[NSDictionary initWithObjects:forKeys:]: count of objects (0) differs from count of keys (2)'

这是我保存plist的代码.

here is my code in saving the plist.

    @interface setting : UIViewController{
    UIDatePicker *datePicker;
    IBOutlet UILabel * morningtime;
    UIDatePicker *afternoonpicker;
    NSString*morningtime1;
    NSString*afternoontime1;
     IBOutlet UILabel *afternoontime;
}
@property (nonatomic,retain) IBOutlet UIDatePicker *datePicker;
@property (strong, nonatomic) IBOutlet UIDatePicker *afternoonpicker;

@property (nonatomic, retain) IBOutlet NSString *morningtime1;

@property (nonatomic, retain) IBOutlet NSString *afternoontime1;
@property (nonatomic, retain) IBOutlet UILabel *morningtime;

@property (nonatomic, retain) IBOutlet UILabel *afternoontime;
@property (weak, nonatomic) IBOutlet UIButton *morning;




- (IBAction)savetext:(id)sender {
    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    // get documents path
    NSString *documentsPath = [paths objectAtIndex:0];
    // get the path to our Data/plist file
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];

    self.morningtime1 = morningtime.text;
    self.afternoontime1 = afternoontime.text;
    NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: morningtime1, afternoontime1, nil] forKeys:[NSArray arrayWithObjects: @"Morning", @"Afternoon", nil]];

    NSString *error = nil;
    // create NSData from dictionary
    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];

    // check is plistData exists
    if(plistData)
    {
        // write plistData to our Data.plist file
        [plistData writeToFile:plistPath atomically:YES];
    }
    else
    {
        NSLog(@"Error in saveData: %@", error);
    }
}

推荐答案

morningtime1几乎可以肯定在这里nil,过早地结束了数组列表.

morningtime1 is almost certainly nil here, prematurely ending the array list.

如果您在此处使用了新的数组文字语法:

If you used the new array literal syntax here:

@[morningtime1, afternoontime1];

您会崩溃,因为将nil分配给NSArray元素是非法的.

you would get a crash, because it's illegal to assign nil to an NSArray element.

这篇关于-[NSDictionary initWithObjects:forKeys:]:对象数(0)与键数(2)不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆