RestKit给我EXC_BAD_ACCESS
问题描述:
我一直在试图让RestKit工作在我的应用程序,但我不成功。我有一个支持ARC的应用程序,我使用https://github.com/RestKit/RestKit/wiki/Installing-RestKit-in-Xcode-4.x进行安装。我一直在阅读关于StackOverflow的文章,说我需要一个单例类或其他东西。我回到了基础知识,并试图按照以下示例进行操作:https://github.com/RestKit/RestKit/wiki/Tutorial-%3A-Introduction-to-RestKit。RestKit给我EXC_BAD_ACCESS
在我的AppDelegate,我有:
RKClient *client = [RKClient clientWithBaseURLString:@"http://restkit.org"];
NSLog(@"I am your RKClient singleton: %@", [RKClient sharedClient]);
在我的其他类中,我有:
- (void) sendRequests {
[[RKClient sharedClient] get: @"/foo.xml" delegate:self];
NSDictionary *params = [NSDictionary dictionaryWithObject:@"RestKit" forKey:@"Sender"];
[ [RKClient sharedClient] post:@"/other.json" params:params delegate:self];
// DELETE a remote resource from the server
[ [RKClient sharedClient] delete:@"/missing_resource.txt" delegate:self];
}
- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {
if ([request isGET]) {
// Handling GET /foo.xml
if ([response isOK]) {
// Success! Let's take a look at the data
NSLog(@"Retrieved XML: %@", [response bodyAsString]);
}
} else if ([request isPOST]) {
// Handling POST /other.json
if ([response isJSON]) {
NSLog(@"Got a JSON response back from our POST!");
}
} else if ([request isDELETE]) {
// Handling DELETE /missing_resource.txt
if ([response isNotFound]) {
NSLog(@"The resource path '%@' was not found.", [request resourcePath]);
}
}
}
- (void) requestWillPrepareForSend:(RKRequest *)request {
NSLog(@"request: %@",request);
}
我得到一个EXC_BAD_ACCESS(代码为-1,地址= 0x766c6550)错误,并它指向行是:
if ([self.delegate respondsToSelector:@selector(requestWillPrepareForSend:)]) {
顺便说一句,我做以下调用调用getRequest:
RestHandler *myRestHandler = [[RestHandler alloc] init];
[myRestHandler sendRequests];
和RestHandler确实有。
任何人都可以在这里提供一些见解吗?我读了一堆关于StackOverflow的文章,但我回到基础知识,仍然无法弄清楚。
谢谢!
答
如果您没有为请求使用本地变量,则可以使用该代码。把RestHandler对象的AppDelegate中的实例变量,这样做:
@interface RSAppDelegate : NSObject <NSApplicationDelegate,NSOutlineViewDataSource, NSOutlineViewDelegate> {
RestHandler *myRestHandler;
}
而在在applicationDidFinishLaunching:
myRestHandler = [[RestHandler alloc] init];
[myRestHandler sendRequests];
答
设置的myRestHandler
强劲的性能。在url请求由Restkit
组成之前,该对象正在释放其内存。
错误发生之前'self.delegate'的价值是什么? (NSLog) –
NSLog(@“self.delegate is:%@”,self.delegate);我得到一个EXC_BAD_ACCESS ..我想 [[RKClient sharedClient] get:@“/ foo.xml”delegate:self]; 会设置I创建了一个静态类委托 – Andy
,现在我看到: 2012-06-01 11:29:11.870 BareTerminalApp [147:707]请求: 2012-06-01 11:29:11.887 BareTerminalApp [147:707]请求: 2012-06-01 11:29:11.893 BareTerminalApp [147:707]请求: 2012-06-01 11: 29:12.311 BareTerminalApp [147:707]我restkit.network:RKRequest.m:676状态代码:404 2012-06-01 11:29:12.319 BareTerminalApp [147:707]我restkit.network:RKRequest.m:676状态码:404 2012-06-01 11:29:12.322 BareTerminalApp [147:707]我restkit.network:RKRequest.m:676状态码:404 T汉克斯! –
Andy