美文网首页iOS Developerios实用开发技巧
Objective-C与Swift混编中遇到fatal erro

Objective-C与Swift混编中遇到fatal erro

作者: ShevaKuilin | 来源:发表于2017-10-17 20:19 被阅读0次

今天在混编时碰到一个关于Swift取值的报错问题,记录一下。

- 语言环境:Swift3+、Objective-C混编

- Xcode环境:9.0

- 报错场景:

Objective-C中
在OC中实现了一个Model类XTNewVoteModel,用作展示数据

@interface XTNewVoteModel : NSObject

@property(nonatomic,strong,readwrite) NSString *objectId;
@property(nonatomic,strong,readwrite) NSString *uid;
@property(nonatomic,strong,readwrite) APIUser *user;
@property(nonatomic,strong,readwrite) NSString *content;
@property(nonatomic,strong,readwrite) NSArray *pictures;
@property(nonatomic,assign,readwrite) NSNumber *commentCount;
@property(nonatomic,assign,readwrite) NSNumber *likedCount;
@property(nonatomic,assign,readwrite) BOOL isLiked;
@property(nonatomic,strong,readwrite) NSString *createdAt;
@property(nonatomic,strong,readwrite) NSString *updatedAt;

@end

然后在请求A的封装类B中利用yymodel解析返回数据到XTNewVoteResultInfo

NSString *jsonStr = [request.responseObject objectForKey:RESPONSE_STATUS_DATA_KEY];
XTNewVoteResultInfo *resultInfo = [XTNewVoteResultInfo yy_modelWithJSON:jsonStr];

注:XTNewVoteModel在XTNewVoteResultInfo中的表现为NSArray<XTNewVoteModel *> *list (这是我最终需要的数据)

@interface XTNewVoteResultInfo : NSObject

@property(nonatomic,assign,readwrite) NSInteger total;
@property(nonatomic,strong,readwrite) NSArray<XTNewVoteModel *> *list;

@end

Swift中
在Swift类中调用网络请求类B,并处理返回的数据。但是在传递参数时,报出了如下错误:
atal error: NSArray element failed to match the Swift Array Element type

这令我十分的费解,明明参数格式、名称都正确,为什么会出现这个error?于是在各种google以及尝试无功而返后,我决定逐步排查问题,在经历了一个多小时的摸爬滚打中,我发现我在Swift中的数组元素本应该是[XTNewVoteModel]格式的,但是却显示的是[Dictionary]类型的,于是我将怀疑范围逐步缩小到了XTNewVoteModel这个Model上面。经过仔细排查,我发现我没有对这个Model进行映射处理,导致Swift无法对这个Oc类中的类型进行正确的处理。
先说解决方案:
在XTNewVoteResultInfo.m中加入如下两段代码即可解决问题:

@implementation XTNewVoteResultInfo

#pragma mark - 字段映射

+ (NSDictionary *)modelCustomPropertyMapper {
    return @{
             @"list":@"list"
             };
}

+ (NSDictionary *)modelContainerPropertyGenericClass {
    return @{
             @"list":[XTNewVoteModel class],
             };
}

@end

这两段代码就是yymodel中声明的方法,调用类通过实现这些方法,实现JSON和Model之间转换过程中的特殊处理。

简单介绍 参考: iOS源码解析—YYModel(NSObject+YYModel)

1.modelCustomPropertyMapper方法,可以指定json和model转化过程中key的映射关系,如果存在以下的字典:

NSDictionary *studentDic = @{@"NAME" : @"Tomy",
                                @"AGE" : @{@"num":@18},
                             @"college" : @{@"name" : @"NJU"}};

如果需要将studentDic转化成Student类型的model,需要实现如下modelCustomPropertyMapper方法:

+ (nullable NSDictionary<NSString *, id> *)modelCustomPropertyMapper
{
    return @{@"name" : @"NAME",
                 @"age" : @"AGE.num"};
}

返回的键值对中的key是Student的property名称,value是studentDic中的key。

2.如果Student中存在property是对象数组或者字典,实现modelContainerPropertyGenericClass方法,例如Student存在属性mobilePhones,维护一组MobilePhone对象

@interface MobilePhone : NSObject
@property (nonatomic, copy) NSString *brand;
@property (nonatomic, assign) NSInteger phoneNumber;
@end

@interface Student : NSObject <YYModel>
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, strong) College *college;
@property (nonatomic, strong) NSArray *mobilePhones; //MobilePhone对象数组
@end
NSDictionary *studentDic = 
  @{@"name" : @"Tomy",
    @"age" : @18,
    @"college" : @{@"name" : @"NJU"},
    @"mobilePhones" : @[@{@"brand" : @"iphone",@"phoneNumber" : @123456},
                        @{@"brand" : @"HUAWEI",@"phoneNumber" : @123456}]};

调用[Student yy_modelWithDictionary:studentDic]方法将studentDic中的mobilePhones转化成Student的mobilePhones属性,需要实现如下:

+ (nullable NSDictionary<NSString*, id>*)modelContainerPropertyGenericClass
{
    return @{@"mobilePhones" : [MobilePhone class]};
}

相关文章

网友评论

    本文标题:Objective-C与Swift混编中遇到fatal erro

    本文链接:https://www.haomeiwen.com/subject/kjhjuxtx.html