Adam Rush
Adam Rush

Reputation: 155

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?

Upvotes: 6

Views: 5246

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726509

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];

Upvotes: 17

Related Questions