Skip to content

Commit

Permalink
Support Objective-C Literals
Browse files Browse the repository at this point in the history
  • Loading branch information
gooichi committed Oct 24, 2013
1 parent ce37270 commit f1cd043
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
26 changes: 13 additions & 13 deletions BZFoursquare/BZFoursquare.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ - (void)dealloc {

- (BOOL)startAuthorization {
NSMutableArray *pairs = [NSMutableArray array];
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:_clientID, @"client_id", @"token", @"response_type", _callbackURL, @"redirect_uri", nil];
NSDictionary *parameters = @{@"client_id": _clientID, @"response_type": @"token", @"redirect_uri": _callbackURL};
for (NSString *key in parameters) {
NSString *value = [parameters objectForKey:key];
NSString *value = parameters[key];
CFStringRef escapedValue = CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)value, NULL, CFSTR("%:/?#[]@!$&'()*+,;="), kCFStringEncodingUTF8);
NSMutableString *pair = [[key mutableCopy] autorelease];
[pair appendString:@"="];
Expand Down Expand Up @@ -103,11 +103,11 @@ - (BOOL)handleOpenURL:(NSURL *)url {
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
for (NSString *pair in pairs) {
NSArray *kv = [pair componentsSeparatedByString:@"="];
NSString *key = [kv objectAtIndex:0];
NSString *val = [[kv objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[parameters setObject:val forKey:key];
NSString *key = kv[0];
NSString *val = [kv[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
parameters[key] = val;
}
self.accessToken = [parameters objectForKey:@"access_token"];
self.accessToken = parameters[@"access_token"];
if (_accessToken) {
if ([_sessionDelegate respondsToSelector:@selector(foursquareDidAuthorize:)]) {
[_sessionDelegate foursquareDidAuthorize:self];
Expand All @@ -131,28 +131,28 @@ - (BOOL)isSessionValid {
- (BZFoursquareRequest *)requestWithPath:(NSString *)path HTTPMethod:(NSString *)HTTPMethod parameters:(NSDictionary *)parameters delegate:(id<BZFoursquareRequestDelegate>)delegate {
NSMutableDictionary *mDict = [NSMutableDictionary dictionaryWithDictionary:parameters];
if (_accessToken) {
[mDict setObject:_accessToken forKey:@"oauth_token"];
mDict[@"oauth_token"] = _accessToken;
}
if (_version) {
[mDict setObject:_version forKey:@"v"];
mDict[@"v"] = _version;
}
if (_locale) {
[mDict setObject:_locale forKey:@"locale"];
mDict[@"locale"] = _locale;
}
return [[[BZFoursquareRequest alloc] initWithPath:path HTTPMethod:HTTPMethod parameters:mDict delegate:delegate] autorelease];
}

- (BZFoursquareRequest *)userlessRequestWithPath:(NSString *)path HTTPMethod:(NSString *)HTTPMethod parameters:(NSDictionary *)parameters delegate:(id<BZFoursquareRequestDelegate>)delegate {
NSMutableDictionary *mDict = [NSMutableDictionary dictionaryWithDictionary:parameters];
[mDict setObject:_clientID forKey:@"client_id"];
mDict[@"client_id"] = _clientID;
if (_clientSecret) {
[mDict setObject:_clientSecret forKey:@"client_secret"];
mDict[@"client_secret"] = _clientSecret;
}
if (_version) {
[mDict setObject:_version forKey:@"v"];
mDict[@"v"] = _version;
}
if (_locale) {
[mDict setObject:_locale forKey:@"locale"];
mDict[@"locale"] = _locale;
}
return [[[BZFoursquareRequest alloc] initWithPath:path HTTPMethod:HTTPMethod parameters:mDict delegate:delegate] autorelease];
}
Expand Down
20 changes: 10 additions & 10 deletions BZFoursquare/BZFoursquareRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
SBJsonParser *parser = [[[SBJsonParser alloc] init] autorelease];
response = [parser objectWithString:responseString];
if (!response) {
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:parser.error forKey:NSLocalizedDescriptionKey];
NSDictionary *userInfo = @{NSLocalizedDescriptionKey: parser.error};
error = [NSError errorWithDomain:@"org.brautaset.SBJsonParser.ErrorDomain" code:0 userInfo:userInfo];
}
#else
Expand All @@ -211,7 +211,7 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
SBJsonParser *parser = [[[SBJsonParser alloc] init] autorelease];
response = [parser objectWithString:responseString];
if (!response) {
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:parser.error forKey:NSLocalizedDescriptionKey];
NSDictionary *userInfo = @{NSLocalizedDescriptionKey, parser.error};
error = [NSError errorWithDomain:@"org.brautaset.SBJsonParser.ErrorDomain" code:0 userInfo:userInfo];
}
#else
Expand All @@ -221,10 +221,10 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
if (!response) {
goto bye;
}
self.meta = [response objectForKey:@"meta"];
self.notifications = [response objectForKey:@"notifications"];
self.response = [response objectForKey:@"response"];
NSInteger code = [[_meta objectForKey:@"code"] integerValue];
self.meta = response[@"meta"];
self.notifications = response[@"notifications"];
self.response = response[@"response"];
NSInteger code = [_meta[@"code"] integerValue];
if (code / 100 != 2) {
error = [NSError errorWithDomain:BZFoursquareErrorDomain code:code userInfo:_meta];
}
Expand Down Expand Up @@ -256,7 +256,7 @@ - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)err
- (NSURLRequest *)requestForGETMethod {
NSMutableArray *pairs = [NSMutableArray array];
for (NSString *key in _parameters) {
NSString *value = [_parameters objectForKey:key];
NSString *value = _parameters[key];
if (![value isKindOfClass:[NSString class]]) {
if ([value isKindOfClass:[NSNumber class]]) {
value = [value description];
Expand Down Expand Up @@ -298,13 +298,13 @@ - (NSURLRequest *)requestForPOSTMethod {
NSData *dashBoundaryData = [dashBoundary dataUsingEncoding:NSUTF8StringEncoding];
NSData *crlfData = [NSData dataWithBytes:"\r\n" length:2];
for (NSString *key in _parameters) {
NSString *value = [_parameters objectForKey:key];
NSString *value = _parameters[key];
if (![value isKindOfClass:[NSString class]]) {
if ([value isKindOfClass:[NSNumber class]]) {
value = [value description];
} else {
if ([value isKindOfClass:[NSData class]]) {
[datas setObject:value forKey:key];
datas[key] = value;
}
continue;
}
Expand Down Expand Up @@ -347,7 +347,7 @@ - (NSURLRequest *)requestForPOSTMethod {
// empty line
[body appendData:crlfData];
// content
[body appendData:[datas objectForKey:key]];
[body appendData:datas[key]];
// CRLF
[body appendData:crlfData];
}
Expand Down

0 comments on commit f1cd043

Please sign in to comment.