Pau*_*ams 1 objective-c restkit ios5 xcode4.2
我正在尝试使用RestKit的RKObjectManager将JSON数据反序列化到我的iPhone应用程序中.
我目前的问题是应用程序崩溃了:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Course 0x6e71b10> valueForUndefinedKey:]: this class is not key value coding-compliant for the key id.'
Run Code Online (Sandbox Code Playgroud)
我打电话的时候:
[manager loadObjectsAtResourcePath:@"/courses" delegate:nil];
Run Code Online (Sandbox Code Playgroud)
我的域类 - Course.h看起来像
#import <Foundation/Foundation.h>
@interface Course : NSObject {
}
@property(nonatomic) NSInteger *id;
@property(nonatomic, retain) NSString *name;
-(id)initWithIdAndName: (NSInteger *)inId inName:(NSString *)inName;
@end
Run Code Online (Sandbox Code Playgroud)
而Course.m看起来像
#import "Course.h"
#import "NSDictionary+RKAdditions.h"
@implementation Course {
}
@synthesize name = _name;
@synthesize id = _id;
- (id)initWithIdAndName:(NSInteger *)inId inName:(NSString *)inName {
_name = inName;
_id = inId;
return self;
}
#pragma mark NSCoding Protocol
- (void)encodeWithCoder:(NSCoder *)encoder;
{
[encoder encodeInteger:[self id] forKey:@"id"];
[encoder encodeObject:[self name] forKey:@"name"];
}
- (id)initWithCoder:(NSCoder *)decoder;
{
if ( ![super init] )
return nil;
[self setId:[decoder decodeIntForKey:@"id"]];
[self setName:[decoder decodeObjectForKey:@"name"]];
return self;
}
@end
Run Code Online (Sandbox Code Playgroud)
从服务器检索的Json数据是
{"courses":[{"id":1,"name":"course 1"},{"id":2,"name":"course 2"}]}
Run Code Online (Sandbox Code Playgroud)
并使用以下代码调用RKObjectManager:
RKObjectMapping* courseMapping = [RKObjectMapping mappingForClass:[Course class]];
[courseMapping mapKeyPath:@"id" toAttribute:@"id"];
[courseMapping mapKeyPath:@"name" toAttribute:@"name"];
RKObjectManager *manager = [RKObjectManager objectManagerWithBaseURL:@"http://firstbit/of/url"];
[manager.mappingProvider setMapping:courseMapping forKeyPath:@"courses"];
[manager loadObjectsAtResourcePath:@"/endpoint" delegate:nil];
Run Code Online (Sandbox Code Playgroud)
关于这个的任何想法?
| 归档时间: |
|
| 查看次数: |
9138 次 |
| 最近记录: |