我在我的应用中创建了一个复选框。如果选中此框,我想设置单词" medium"保存到我的数据库字段。在我的"如果checked1 = YES"阻止,我创建了一个NSString并将其设置为" Medium"。也就是说,在我的提交按钮块中,我应该使用哪种代码来保存设置字符串?
从我的数据库返回的JSON的结构需要以下代码才能输入字符串,但是我不知道如何重写它以适应NSString(我以前用过NSArray)?
NSDictionary *bodyValues = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:medium, nil] forKeys:[NSArray arrayWithObjects:@"value", nil]];
NSDictionary *languageDict = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:bodyValues] forKey:@"und"];
[nodeData setObject:languageDict forKey:@"size"];
运行此块时出现的错误是:
*由于未捕获的异常终止应用' NSInvalidArgumentException',原因:' * - [NSDictionary initWithObjects:forKeys:]:对象的数量(0)与count的数量不同 键(1)'
请参阅下面的代码 - 希望我能清楚地解释这一点,并且谢谢:
·H
@property (weak, nonatomic) IBOutlet NSString *medium;
的.m
- (IBAction)checkButton:(id)sender {
if (!checked1) {
[checkboxButton setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
checked1 = YES;
NSString *medium = @"Medium";
NSLog(@"button 1");
}
else if (checked1) {
[checkboxButton setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
checked1 = NO;
}
}
- (IBAction)submitPet:(id)sender {
NSMutableDictionary *nodeData = [NSMutableDictionary new];
[nodeData setValue: self.dogsName.text forKey:@"title"];
[nodeData setObject:@"pets" forKey:@"type"];
NSDictionary *bodyValues = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:medium, nil] forKeys:[NSArray arrayWithObjects:@"value", nil]];
NSDictionary *languageDict = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:bodyValues] forKey:@"und"];
[nodeData setObject:languageDict forKey:@"size"];
[DIOSNode nodeSave:nodeData success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Node saved!");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Node did not save!");
}];
}
答案 0 :(得分:0)
我认为您正在通过nil
进行dictionaryWithObjects
次来电。您无法将nil
添加到词典中。使用nslog
在变量添加之前打印出来。