Saturday, 7 September 2013

Second object of each pair must be non-nil. NSDictionary

Second object of each pair must be non-nil. NSDictionary

Below is the error that I am getting.
* Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '+[NSDictionary dictionaryWithObjectsAndKeys:]: second object of
each pair must be non-nil. Or, did you forget to nil-terminate your
parameter list?'
The error is triggered when running the code below the comment //second
attempt.
BOOL techNews = _technologyButton.selected;
BOOL worldAffairsNews = _worldAffairsButton.selected;
BOOL sportsNews = _sportsButton.selected;
BOOL politicsNews = _politicsButton.selected;
BOOL economicsNews = _economicsButton.selected;
[[NSUserDefaults standardUserDefaults] setBool:techNews
forKey:@"techNews"];
[[NSUserDefaults standardUserDefaults] setBool:sportsNews
forKey:@"sportsNews"];
[[NSUserDefaults standardUserDefaults] setBool:politicsNews
forKey:@"politicsNews"];
[[NSUserDefaults standardUserDefaults] setBool:worldAffairsNews
forKey:@"worldAffairNews"];
[[NSUserDefaults standardUserDefaults] setBool:economicsNews
forKey:@"economicsNews"];
NSString *email = [[NSUserDefaults standardUserDefaults]
stringForKey:@"email"];
NSString *phash = [[NSUserDefaults standardUserDefaults]
stringForKey:@"phash"];
NSLog(@"%@", email);
NSLog(@"%@", phash);
NSNumber *techB = [NSNumber numberWithBool:techNews];
NSNumber *worldB = [NSNumber numberWithBool:worldAffairsNews];
NSNumber *sportsB = [NSNumber numberWithBool:sportsNews];
NSNumber *politicsB = [NSNumber numberWithBool:politicsNews];
NSNumber *economicsB = [NSNumber numberWithBool:economicsNews];
//Second Attempt
NSDictionary *params = [NSDictionary
dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], nil];
params = @{@"email": email ?: @"",
@"phash": phash ?: @"",
@"tNews": techB ?: @"",
@"wNews": worldB ?: @"",
@"sNews": sportsB ?: @"",
@"pNews": politicsB ?: @"",
@"eNews": economicsB ?: @""};
NSOperationQueue *queue= [[NSOperationQueue alloc]init];
NSString *urlPath = [NSString stringWithFormat:@"http://54.221.224.251"];
NSURL *url = [NSURL URLWithString:urlPath];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8"
forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[self httpBodyForParamsDictionary:params]];
[NSURLConnection sendAsynchronousRequest:request queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError
*error) {
if (error){
NSLog(@"sendAsynchronousRequest error = %@", error);
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
int code = [httpResponse statusCode];
NSString *coder = [NSString stringWithFormat:@"%d",code];
NSLog(@"%@",coder);
}
if (data) {
NSString *dataStr = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(@"This is Data: %@",dataStr);
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
int code = [httpResponse statusCode];
NSString *coder = [NSString stringWithFormat:@"%d",code];
NSLog(@"%@",coder);
}
}];
}
Below are some methods that I have included that may or may not be useful.
@implementation NSString (PercentEscape)
- (NSString *)stringForPostParameterValue:(NSStringEncoding)encoding
{
NSString *string =
CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)self,
(CFStringRef)@"
",
(CFStringRef)@";/?:@&=+$,",
CFStringConvertNSStringEncodingToEncoding(encoding)));
return [string stringByReplacingOccurrencesOfString:@" "
withString:@"+"];
}
@end
- (NSData *)httpBodyForParamsDictionary:(NSDictionary *)paramDictionary
{
NSMutableArray *paramArray = [NSMutableArray array];
[paramDictionary enumerateKeysAndObjectsUsingBlock:^(NSString *key,
NSString *obj, BOOL *stop) {
NSString *param = [NSString stringWithFormat:@"%@=%@", key, [obj
stringForPostParameterValue:NSUTF8StringEncoding]];
[paramArray addObject:param];
}];
NSString *string = [paramArray componentsJoinedByString:@"&"];
return [string dataUsingEncoding:NSUTF8StringEncoding];
}
The following code I use else ware in my code, and it works perfectly,
throwing no exceptions, and setting the variables correctly.
NSDictionary *params = @{@"name" : _nameField.text ?: @"", @"email":
_emailField.text ?: @"", @"phash": [NSString
stringWithFormat:@"%d",phashnum]};

No comments:

Post a Comment