在我的应用程序中,用户可以创建 UITextFields。向每个字段添加一个标签,以便标签对应于以下情况:1、2、3、4,...然后我将所有内容添加到 NSDictionary 和 json 表示中:
-(IBAction)buttonDropBoxuploadPressed:(id)sender{
//screenshot
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy_MM_dd"];
NSString *filename = [NSString stringWithFormat:@"By: %@ ",
[formatter stringFromDate:[NSDate date]]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat:@"%@", filename] ];
//[data writeToFile:path atomically:YES];
//NSString *destDir = @"/sandbox/";
// [[self restClient] uploadFile:filename toPath:destDir
// withParentRev:nil fromPath:path];
// [[self restClient] loadMetadata:@"/sandbox/"];
//JSON
NSString *object;
NSString *object2;
NSString *object3;
NSString *object4;
NSString *object5;
NSString *object6;
NSString *object7;
NSString *object8;
NSString *object9;
NSString *object10;
NSString *object11;
NSString *object12;
NSString *object13;
NSString *object14;
NSString *object15;
for (UITextField *text in messagename) {
int touchedtag = text.tag;
NSUInteger tagCount = touchedtag;
switch (tagCount) {
case 1:
object = [NSString stringWithFormat:@"%@", text.text];
break;
case 2:
object2 = [NSString stringWithFormat:@"%@", text.text];
break;
case 3:
object3 = [NSString stringWithFormat:@"%@", text.text];
break;
case 4:
object4 = [NSString stringWithFormat:@" %@", text.text];
break;
case 5:
object5 = [NSString stringWithFormat:@"%@", text.text];
break;
case 6:
object6 = [NSString stringWithFormat:@"%@", text.text];
break;
case 7:
object7 = [NSString stringWithFormat:@"%@", text.text];
break;
case 8:
object8 = [NSString stringWithFormat:@"%@", text.text];
break;
case 9:
object9 = [NSString stringWithFormat:@"%@", text.text];
break;
case 10:
object10 = [NSString stringWithFormat:@"%@", text.text];
break;
case 11:
object11 = [NSString stringWithFormat:@"%@", text.text];
break;
case 12:
object12 = [NSString stringWithFormat:@"%@", text.text];
break;
case 13:
object13 = [NSString stringWithFormat:@"%@", text.text];
break;
case 14:
object14 = [NSString stringWithFormat:@"%@", text.text];
break;
case 15:
object15 = [NSString stringWithFormat:@"%@", text.text];
break;
default :
break;
}
}
//arrays
NSString * objects[] = { object, object2, object3, object4, object5, object6, object7, object8, object9, object10, object11, object12, object13, object14, object15};
NSMutableArray *textnameobject = [[NSMutableArray alloc] initWithCapacity:b];
textnameobject = [NSMutableArray arrayWithObjects:objects count:b];
NSMutableArray *textnamekeys = [[NSMutableArray alloc] initWithCapacity:b];
NSString * textnumber[] = {@"title", @"title", @"title",@"title", @"title", @"title", @"title", @"title", @"title", @"title", @"title", @"title", @"title", @"title"};
textnamekeys = [NSMutableArray arrayWithObjects:textnumber count:b];
//arrays
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObject: textnameobject forKey:textnamekeys];
/*
NSArray *objects2 = [NSArray arrayWithObjects:jsonDictionary, nil];
NSArray *keys2 = [NSArray arrayWithObjects:allkeys, nil];
NSDictionary *mainDict = [NSDictionary dictionaryWithObjects:objects2 forKeys:keys2];
*/
NSString* jsonString = [jsonDictionary JSONRepresentation];
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
[jsonData writeToFile:path atomically:YES];
NSString *destDir = @"/sandbox/";
[[self restClient] uploadFile:filename toPath:destDir
withParentRev:nil fromPath:path];
[[self restClient] loadMetadata:@"/sandbox/"];
//JSON
}
当我按下按钮时,我收到以下错误:
JSON 表示失败。错误跟踪是:( "Error Domain=org.brautaset.JSON.ErrorDomain Code=1 \"JSON object key must be string\" UserInfo=0x2e8370 {NSLocalizedDescription=JSON object key must be string}" )
并因此导致保管箱错误。这适用于我以前的应用程序,代码完全相同。正确添加了 json 库。看不懂!!请帮忙!