我是iOS开发的新手,我正在使用ARC测试带有xCode 4.2的iOS的RestKit 0.9.3,我遇到了一个简单的get请求的问题。
我遵循本教程:https://github.com/RestKit/RestKit/wiki/Tutorial-%3A-Introduction-to-RestKit
我尝试在TouchUpInside
和UIButton
上向我的网络服务发送一个简单的获取请求。
但我收到了“EXC_BAD_ACCESS”:[6373:fb03] *** -[DataAccess respondsToSelector:]: message sent to deallocated instance 0x8275160
应用程序在RKRequest.m
文件:
if ([self.delegate respondsToSelector:@selector(requestDidStartLoad:)]) {
[self.delegate requestDidStartLoad:self];
}
我的代码:
MyViewController.m:
- (IBAction)myAction:(id)sender {
DataAccess *data = [DataAccess alloc];
[data sendRequests];
}
DataAccess.m:
@implementation DataAccess
-(void)sendRequests {
[RKClient clientWithBaseURL:SERVER_URL username:SERVER_USERNAME password:SERVER_PASSWORD];
[[RKClient sharedClient] get:@"/myDistantAction" delegate:self];
}
#pragma mark - Delegate
-(void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response {
if ([response isOK]) {
NSLog(@"Retrieved : %@", [response bodyAsString]);
}
}
@end
我在互联网上搜索但我找不到解决方案
有人可以帮助我吗?
谢谢,
答案 0 :(得分:5)
这可能是一种解决方案。我改变了你的代码以使用单例。我认为问题在于调用回调函数时,因为他无法再访问该实例。
DataAccess.m:
@implementation DataAccess
static singleton *DataAccess= nil;
+ (DataAccess*)getInstance
{
if (singleton == nil) {
singleton = [[DataAccess alloc] init];
}
return singleton;
}
-(void)sendRequests {
[RKClient clientWithBaseURL:SERVER_URL username:SERVER_USERNAME password:SERVER_PASSWORD];
[[RKClient sharedClient] get:@"/myDistantAction" delegate:self];
}
#pragma mark - Delegate
-(void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response {
if ([response isOK]) {
NSLog(@"Retrieved : %@", [response bodyAsString]);
}
}
@end
MyViewController.m:
- (IBAction)myAction:(id)sender {
DataAccess *data = [DataAccess getInstance];
[data sendRequests];
}