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

自作多情 提交于 2019-12-12 18:23:21

问题


I am getting the error of

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

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);
    }
}

回答1:


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

If you used the new array literal syntax here:

@[morningtime1, afternoontime1];

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




回答2:


This line:

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

should be:

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

In other words, reference the properties, not the ivars. As you have it, the morningtime1 ivar is never set (you set the property which is actually setting the generated ivar named _morningtime1).

Side note:

Get rid of any explicit ivars and @synthesize lines for the properties. This will avoid such confusion.



来源:https://stackoverflow.com/questions/15909714/nsdictionary-initwithobjectsforkeys-count-of-objects-0-differs-from-coun

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!