2013-12-07 104 views
-1

任何使用restkit的人都会将布尔值传递回服务器?我可以传回字符串“真”还是“假”?如何通过Restkit传递布尔值

服务器将该属性存储为布尔值。

+0

为[在你的另一个问题讨论](http://stackoverflow.com/questions/20445375/collection-element-of -type-bool-is-not-an-objective-c-object),请在你的字典中使用'@(YES)'或'@(NO)'。或者如果你有'BOOL'变量,比如'isExclusive',使用'@(isExclusive)'。 – Rob

+0

是否超过1和0?服务器不期望1或0。 – jdog

回答

1

RestKit使用NSJSONSerialization。如果你有一个布尔变量,比如说isExclusive,你可以使用[NSNumber numberWithBool:isExclusive]或布尔文本@(isExclusive)将它添加到你的集合对象中。 NSJSONSerialization将根据情况将其表示为truefalse

例如:

BOOL   isExclusive = YES; 
NSDictionary *dictionary = @{@"exclusive" : @(isExclusive)}; 
NSData  *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error]; 
NSString  *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 
NSLog(@"JSON = %@", jsonString); 

这将报告:

 
JSON = {"exclusive":true}