gpt4 book ai didi

mysql - 带有我的 JSON 数据的 RestKit 不工作

转载 作者:行者123 更新时间:2023-11-29 03:18:56 26 4
gpt4 key购买 nike

我正在尝试了解 restkit 0.22。我从不同的博客和 YouTube 上获得了一些教程。我最终混淆了代码。

谁能帮我解决这个问题,我真的需要它来为我的项目工作。

我用实体 Songs.xcdatamodeld 创建了核心数据模型

我有一个来 self 的 mySQL 数据库的 json:

[{"SongID":"1","SongTitle":"Song1","PerformerName":"Performer1","SongURL":"http://mysite/mysongs/1.mp3","PerformerPic":"PerfPic1.png"},
{"SongID":"2","SongTitle":"Song2","PerformerName":"Performer2","SongURL":"http://mysite/mysongs/2.mp3","PerformerPic":"PerfPic2.png"},
{"SongID":"3","SongTitle":"Song3","PerformerName":"Performer3","SongURL":"http://mysite/mysongs/3.mp3","PerformerPic":"PerfPic3.png"}]

在 AppDelegate.m 中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:http://mysite]];
NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Songs" ofType:@"momd"]];

//Initialize managed object store
NSManagedObjectModel *managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL ] mutableCopy];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];

objectManager.managedObjectStore = managedObjectStore;
[RKObjectManager sharedManager].requestSerializationMIMEType = RKMIMETypeJSON;
[RKMIMETypeSerialization registeredMIMETypes];
[objectManager setAcceptHeaderWithMIMEType:@"application/json"];

RKEntityMapping* mapping = [RKEntityMapping mappingForEntityForName:@"Songs"
inManagedObjectStore:[RKObjectManager sharedManager].managedObjectStore];

mapping.identificationAttributes = @[@"songID"];

[mapping addAttributeMappingsFromDictionary:@{@"id" : @"SongID",
@"songTitle" : @"SongTitle",
@"performerName" : @"PerformerName",
@"songURL" : @"SongURL",
@"performerPic" : @"PerformerPic"}];


return YES;
}

在 TableView Controller 中:

- (void)viewDidLoad
{
[super viewDidLoad];

[RKObjectManager.sharedManager getObjectsAtPath:@"/api.php"
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
{
self.Songs = [mappingResult array];

NSLog(@"It Worked: %@", self.Songs);

} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"It Failed: %@", error);
}];

}

这是我遇到的错误:

GET 'http://mysite/api.php' (200 OK / 0 objects) [request=3.5566s mapping=0.0000s
total=3.5627s]: Error Domain=org.restkit.RestKit.ErrorDomain Code=1001
"No response descriptors match the response loaded." UserInfo=0xb5920b0
{NSErrorFailingURLStringKey=http://mysite/api.php, NSLocalizedFailureReason=A 200
response was loaded from the URL 'http://mysite/api.php', which failed to match all (0)
response descriptors:, NSLocalizedDescription=No response descriptors match the response
loaded., keyPath=null, NSErrorFailingURLKey=http://mysite/api.php,
NSUnderlyingError=0xb5921b0 "No mappable object representations were found at the key
paths searched."}

最佳答案

首先,您似乎没有将创建托管对象上下文作为设置代码的一部分。在您解决“主要”问题后,这可能会导致您出现问题:

错误消息中非常清楚地描述了您的主要问题:

which failed to match all (0) response descriptors

即您还没有创建任何 response descriptors .

The Object-mapping guide引导您完成映射和描述符创建过程(并包含更多要启动的详细信息)。


从这样的事情开始:

RKResponseDescriptor *rd = [RKResponseDescriptor responseDescriptorWithMapping:mapping pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

然后将映射添加到对象管理器。

(确保您在某处调用 createManagedObjectContexts)。

关于mysql - 带有我的 JSON 数据的 RestKit 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21359731/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com