这是我的代码的一部分:
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = [[UIScreen mainScreen] bounds];
_webView = [[UIWebView alloc] initWithFrame:frame];
[_webView setHidden:NO];
[self.view addSubview:_webView];
_vk = [[DPVkontakteCommunicator alloc] initWithWebView:_webView];
DPVkontakteUserAccount *user;
NSString *accessToken = [[NSUserDefaults standardUserDefaults]
objectForKey:@"accessToken"];
NSInteger userId = [[[NSUserDefaults standardUserDefaults]
objectForKey:@"userId"] integerValue];
user = [[DPVkontakteUserAccount alloc]
initUserAccountWithAccessToken:accessToken
userId:userId];
NSLog(@"%@", user);
[user setSuccessBlock:^(NSDictionary *dictionary)
{
NSLog(@"%@", dictionary);
}];
NSDictionary *options = @{@"uid":@"1"};
// [user usersGetWithCustomOptions:@{@"uid":@"1"}]; // Zombie
[user usersGetWithCustomOptions:options]; // Not zombie
// __block NSDictionary *options = @{};
//
// [_vk startOnCancelBlock:^{
// NSLog(@"Cancel");
// } onErrorBlock:^(NSError *error) {
// NSLog(@"Error: %@", error);
// } onSuccessBlock:^(DPVkontakteUserAccount *account) {
// NSLog(@"account:%@", account);
//
// [account setSuccessBlock:^(NSDictionary *dictionary)
// {
// NSLog(@"%@", dictionary);
// }];
//
// [account docsGetUploadServerWithCustomOptions:options];
// }];
}
这是处理 userGetWithCustomOptions: 方法的部分:
- (void)forwardInvocation:(NSInvocation *)anInvocation
{
NSString *methodName = NSStringFromSelector([anInvocation selector]);
NSDictionary *options;
[anInvocation getArgument:&options
atIndex:2];
NSArray *parts = [self parseMethodName:methodName];
NSString *vkURLMethodSignature = [NSString stringWithFormat:@"%@%@.%@",
kVKONTAKTE_API_URL,
parts[0],
parts[1]];
// appending params to URL
NSMutableString *fullRequestURL = [vkURLMethodSignature mutableCopy];
[fullRequestURL appendString:@"?"];
[options enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop)
{
[fullRequestURL appendFormat:@"%@=%@&", key, [obj encodeURL]];
}];
[fullRequestURL appendFormat:@"access_token=%@", _accessToken];
// performing HTTP GET request to vkURLMethodSignature URL
NSURL *url = [NSURL URLWithString:fullRequestURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation;
operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:urlRequest
success:^(NSURLRequest *request,
NSHTTPURLResponse *response,
id JSON)
{
_successBlock(JSON);
}
failure:^(NSURLRequest *request,
NSHTTPURLResponse *response,
NSError *error,
id JSON)
{
_errorBlock(error);
}];
[operation start];
}
问题是当我使用“选项”变量时 - 它工作正常,但是当使用直接值时 - 它失败,应用程序崩溃。使用 Profile 我发现该方法调用指向已释放的对象。
为什么会发生这种情况?没有其他代码可以提供帮助。
ViewController.m 代码:https://gist.github.com/AndrewShmig/5398546
DPVkontakteUserAccount.m:https ://gist.github.com/AndrewShmig/5398557