Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Sending BOOL value to server using JSON

I am basically trying to do this;

// Catch the true/false flag and convert to BOOL value for server
NSString *on = [prefs objectForKey:@"published"];
BOOL flag = TRUE;
if ([on isEqualToString:@"true"]) 
{
    flag = TRUE;
} 
else 
{
    flag = FALSE;
}

NSArray *objects = [NSArray arrayWithObjects:body, flag, user_token, request_token, deviceID, api_user, nil];
NSArray *keys = [NSArray arrayWithObjects:@"selections", @"published", @"user_token", @"request_token", @"device_id", @"api_user", nil];
NSDictionary *questionDict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

Which checks if the prefs is true and if so converts to BOOL value with true etc..

However it's crashing saying it's missing count of objects to keys.

Any help with doing this?

like image 928
Adam Rush Avatar asked Jan 08 '14 09:01

Adam Rush


1 Answers

You cannot add primitives to NSArray - Cocoa collections can contain only objects. Wrap your flag in an NSNumber to comply with this requirement, like this:

NSArray *objects = [NSArray arrayWithObjects:body, [NSNumber numberWithBool:flag], user_token, request_token, deviceID, api_user, nil];

You can also use the new syntax for arrays and values to shorten your code:

NSArray *objects = @[body, @(flag), user_token, request_token, deviceID, api_user];
like image 142
Sergey Kalinichenko Avatar answered Oct 03 '22 06:10

Sergey Kalinichenko