- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Mantle
框架,我似乎在将某些值序列化为 MTLModel
时遇到了一些问题。这是我从服务器收到的 JSON
:
{
"id":50,
"name":"UserName",
"email":"user@username.com",
"profile":{
"picture": {
"original": "http://original.com/picture",
"versions": {
"thumb": "http://thumb.com/picture",
"small": "http://small.com/picture"
}
}
}
}
我已经按照以下方式设置了我的 MTLModel
:
用户
@interface User : MTLModel <MTLJSONSerializing>
@property (nonatomic, readonly) NSNumber *id;
@property (nonatomic) NSString *name;
@property (nonatomic) NSString *email;
@property (nonatomic) Profile *profile;
@end
@implementation User
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{
@"id": @"id",
@"name": @"name",
@"email": @"email",
@"profile": @"profile"
};
}
- (NSValueTransformer *)profileJSONTransformer {
return [MTLValueTransformer reversibleTransformerWithForwardBlock:^(NSDictionary *profileDict) {
return [MTLJSONAdapter modelOfClass:Profile.class
fromJSONDictionary:profileDict
error:nil];
} reverseBlock:^(Profile *profile) {
return [MTLJSONAdapter JSONDictionaryFromModel:profile];
}];
}
@end
简介
@interface Profile : MTLModel <MTLJSONSerializing>
@property (nonatomic) Picture *picture;
@end
@implementation Profile
+ (NSDictionary *)JSONKeyPathsByPropertyKeys {
return @{
@"picture": @"picture"
};
}
- (NSValueTransformer *)pictureJSONTransformer {
return [MTLValueTransformer reversibleTransformerWithForwardBlock:^(NSDictionary *picDict) {
return [MTLJSONAdapter modelOfClass:Picture.class
fromJSONDictionary:picDict
error:nil];
} reverseBlock:^(Picture *picture) {
return [MTLJSONAdapter JSONDictionaryFromModel:picture];
}];
}
@end
图片
@interface Picture : MTLModel <MTLJSONSerializing>
@property (nonatomic) NSString *original;
@property (nonatomic) Versions *versions;
@end
@implementation Picture
+ (NSDictionary *)JSONKeyPathsByPropertyKeys {
return @{
@"original": @"original",
@"versions": @"versions"
};
}
- (NSValueTransformer *)versionsJSONTransformer {
return [MTLValueTransformer reversibleTransformerWithForwardBlock:^(NSDictionary *versionDict) {
return [MTLJSONAdapter modelOfClass:Versions.class
fromJSONDictionary:versionDict
error:nil];
} reverseBlock:^(Versions *versions) {
return [MTLJSONAdapter JSONDictionaryFromModel:versions];
}];
}
@end
版本
@interface Versions : MTLModel <MTLJSONSerializing>
@property (nonatomic) NSString *thumb;
@property (nonatomic) NSString *small;
@end
@implementation Versions
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{
@"thumb": @"thumb",
@"small": @"small"
};
}
@end
我正在执行以下 Overcoat
POST 调用以获取 JSON。当我 NSLog
响应时,我收到 JSON 正常:
[[Client getInstance] POST:@"authorize.json" parameters:params completion:^(id response, NSError *error) {
if (!error) {
User *user = [MTLJSONAdapter modelOfClass:[User class] fromJSONDictionary:[response result] error:nil];
Picture *picture = [[user profile] picture];
NSLog(@"%@", picture);
} else {
NSLog(@"%@", error);
}
}];
当我尝试像这样抓取 Picture
对象时出现问题:
Picture *picture = [[user profile] picture];
我得到一个异常(exception):
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary picture]: unrecognized selector sent to instance 0x7f95026d4210'
我该如何解决这个问题?我做错了什么?
最佳答案
您没有正确解析您的 json。图片字典嵌套在配置文件中。得到它喜欢的东西
Nsmutabledictinary *profile=[jsondic objectforkey:@"Profile"];
Nsmutabledictinary *picture=[profile objectforkey:@"Picture"];
请更正语法错误,因为我没有在编辑器中编写代码,因此代码中可能存在语法错误。谢谢
关于ios - 地幔 : Uncaught exception 'NSInvalidArgumentException' when parsing JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32345137/
我在这里撕扯我的头发。已阅读许多描述如何解决此问题的示例,但没有一个可以帮助我解决该问题。 在 UIView 上有一个链接到 IBAction 的简单按钮。 代码是这样的... 联系方式.h #imp
我在 map 中有一些注解 View ,我希望通过touchUpInside打开一个新的ViewController,但出现此错误: Terminating app due to uncaught
我想尝试一下群众的智慧……因为我现在很沮丧。提前致谢。 这是我的代码: - (void)imagePickerController:(UIImagePickerController *)ipc did
我遇到以下问题。我继承了一个应用程序,我正在尝试修改它。原始功能允许我添加带有一些文本注释的任务。我想添加一个图标以使其更具视觉吸引力。当我修改代码签名以传递图标时(实际上我只是在 NSString
[__NSArrayM relatedObjectDidChange]: unrecognized selector sent to instance error Class: NSInvalidAr
我有两个viewController,都带有一个表,应用程序可以工作,然后我在第二个viewController 中添加了导航栏,让他有机会在第一个viewController 中返回。导航栏可以工作
通常应用程序运行良好,但我在 Crashlytics 中收到问题。一周发生1-2次。适合 2k-3k 用户。应用程序适用于 iOS 7 及更高版本。报告:致命异常:NSInvalidArgumentE
此函数旨在获取 JSON 并根据作为参数发送的对象创建对象数组: + (NSArray *)Object: (id) object FromJSON:(NSData *)objectNotation
尝试将核心数据对象获取到tableview并获得Terminating app due to uncaught exception 'NSInvalidArgumentException', reas
我设计了一个应用程序,它将有一个按钮和三个标签。 当按下按钮时,将调用一个URL。 URL的JSON表示如下: [ { "_id": "50c87e8a1898044ea1458e4c",
我以编程方式实现了自动布局。 self.rightSideLine = [[UIView alloc] init]; self.rightSideLine.backgroundColor = COLO
我有一个 iOS 应用程序,可以在 Appstore 中找到。今天我测试了该应用程序,当应用程序尝试打开错误对话框时,它在我的 iPhone4 (iOS7) 上崩溃了。在 iPhone5s 上没有问题
我在其他地方使用“正常工作”的代码时遇到错误: [Error]: Caught "NSInvalidArgumentException" with reason "*** -[_NSPlacehold
我正在编写代码以向图片添加滤镜。这是代码 func applyFilter(index: Int){ print("Applying Filter....") //let contex
我正在练习创建一个主要不使用 Storyboard的 UI,当我开始调试时,我的应用程序崩溃了,并给我带来了这个错误。 我尝试从 info.plist 和目标中删除“main”,但不起作用 2019-
我有一个 tableView,我在一个单元格上创建了一个 Button。 UIButton *deleteGroupButton = [UIButton buttonWithType:UIButt
这是我的代码: @interface TabRegsViewController () @end @implementation TabRegsViewController @synthesize p
即使在这个项目之前我从未在 Objective-C 中工作过或制作过 iOS 应用程序,但我处于必须支持其他人代码的“令人敬畏”的位置,所以如果我做了一些非常明显或愚蠢的事情,请原谅. 我需要一个带有
我的代码抛出以下异常: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[_
我有一个无法理解的问题,我正在使用一个用于使用Map Open Source Map(https://github.com/route-me/route-me)的库,该示例运行完美,并且我相信不是与代
我是一名优秀的程序员,十分优秀!