我一直在努力让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上的示例进行操作。
在我的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上阅读了一些文章,但我回到了基础知识,仍然无法弄明白。
谢谢!
答案 0 :(得分:1)
如果您不对请求使用局部变量,则可以使用该代码。将RestHandler对象放在AppDelegate实例变量上,并执行以下操作:
@interface RSAppDelegate : NSObject <NSApplicationDelegate,NSOutlineViewDataSource, NSOutlineViewDelegate> {
RestHandler *myRestHandler;
}
在applicationDidFinishLaunching中:
myRestHandler = [[RestHandler alloc] init];
[myRestHandler sendRequests];
答案 1 :(得分:0)
将myRestHandler
的属性设置为强。在由Restkit
形成网址请求之前,该对象正在丢失其内存。