我刚开始接触目标C和Restkit代表不工作(Restkit有关?)
我创建了一个示例应用程序,并添加了RKRequestDelegate在MyAppDelegate文件
@interface MyAppDelegate : NSObject <UIApplicationDelegate, RKRequestDelegate> {…
,并添加
RKClient* client = [RKClient clientWithBaseURL:@"http://localhost:3000"];
NSLog(@"I am your RKClient singleton : %@", [RKClient sharedClient]);
[client get:@"/titles.json" delegate:self];
在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method
到MyAppDelegate.m
我还添加了一个方法来MyAppDelegate.m
- (void) request: (RKRequest *) request didLoadResponse: (RKResponse *) response {
if ([request isGET]) {
NSLog (@"Retrieved : %@", [response bodyAsString]);
}
}
到目前为止好一切工作,我看到在输出从我的Rails应用程序的结果!
由于这些东西不属于MyAppDelegate.m,我将这些东西放入我的模型中。在我Titles.h我加
@interface Titles : NSManagedObject <RKRequestDelegate> {
和Titles.m我加
+ (void) update {
[[RKClient sharedClient] get:@"/titles.json" delegate:self];
}
和
- (void) request: (RKRequest *) request didLoadResponse: (RKResponse *) response {
if ([request isGET]) {
NSLog (@"Retrieved : %@", [response bodyAsString]);
}
}
在我MyAppDelegate.m我代替:
RKClient* client = [RKClient clientWithBaseURL:@"http://localhost:3000"];
NSLog(@"I am your RKClient singleton : %@", [RKClient sharedClient]);
[client get:@"/titles.json" delegate:self];
与
RKClient* client = [RKClient clientWithBaseURL:@"http://localhost:3000"];
NSLog(@"I am your RKClient singleton : %@", [RKClient sharedClient]);
[Titles update];
当我现在运行我没有得到任何输出。 我把几个断点,一个在- (void)didFinishLoad:(RKResponse*)response
在RKRequest文件 ,那边如果测试:
if ([_delegate respondsToSelector:@selector(request:didLoadResponse:)]) {
[_delegate request:self didLoadResponse:finalResponse];
}
,而它在我的第一次尝试成功失败(当一切都在MyAppDelegate)
我检查调试器中的变量_delate和它说:_delegate = MyAppDelegate在我的第一次尝试和_delegate =在我第二次尝试的标题(都喜欢它应该)
为什么该respondsToSelector失败? (委托是正确的,方法在标题中存在)
非常感谢罗布,我做了一点 测试通过使'更新'实例方法和创建一个对象 标题*标题= [标题分配]; [标题更新]; ,它的工作原理。 但是我不明白你对DataModel类的解释。你能否详细说明。我的课堂标题是NSManagedObject(使用核心数据),它将存储不同相册的所有标题。我使用类方法的原因是,像我在测试中那样制作一个对象是愚蠢而不是好设计。 – Glenn
这是一个奇怪的设计。为什么你有一个特殊的NSManagedObject包含其他对象的属性?这似乎与Core Data有关。我想你会有一个'Collection'或'AlbumCollection'来容纳'Album'对象。然后你可以使用'[collection allTitles]'返回标题,如果你需要它作为一个字符串列表。 –
我的解释不是很明显... 我有以下类:艺术家(属性:名称,stage_name,生日,...)和关系has_and_belongs_to_many专辑,专辑(属性:名称,年份,封面)has_many标题...所有这些都是NSManagedObjects。我希望他们每个人都有一个类方法更新,我会在应用程序的开始时调用它。该方法会将数据与Rails应用程序同步。 – Glenn