【问题标题】:Possible to combine two user classes in Objective-C?可以在 Objective-C 中组合两个用户类吗?
【发布时间】:2016-06-17 21:20:12
【问题描述】:

我有两个同名的课程。我因此而发生冲突。 我无法重命名,因为 JSON 模型正在使用该名称以及其他原因,超过 100 个类正在导入它。

它们可以合并吗?还是 JSONModel 的解决方案?

第一类

#import "User.h"


@implementation User


+(User*)createWithDictionary:(NSDictionary*)dictionary inContext:(NSManagedObjectContext*)context{
//Censored
}
+(User*)createWithDictionary:(NSDictionary*)dictionary{
//Censored
}
-(void)updateWithDictionary:(NSDictionary*)dictionary{

//Censored
}
 -(NSString*)classOfProperty:(NSString*)propName{
//Censored
}


@end

2 级

@implementation User
+(JSONKeyMapper*)keyMapper {
return [[JSONKeyMapper alloc] initWithDictionary:@{@"id": @"identifier",
                                                   @"first": @"firstName",
                                                   @"last": @"lastName",
                                                   }];

【问题讨论】:

  • 当然可以合并,但是否应该合并只有你自己说了算。
  • 完全不清楚您要在这里做什么。为什么你有两个同名的类?您最近是否将 JSONModel 添加到您的项目中?您希望现有的 User 类能够由 JSONModel 创建,还是希望 JSON 输入文件中的“用户”映射到另一个类?

标签: objective-c json xcode jsonmodel


【解决方案1】:

当我们遇到这个问题时,我们使用#define 解决了它,并确保在每个源文件中导入正确的包含文件。这是一个糟糕的解决方案,但运输也是一个功能。类似的东西:

#define User MyUser

重命名两者之一。这是正确的做法。

【讨论】:

  • 嗨,Eric,您能否详细说明或提供更多示例
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-11
  • 2016-10-29
相关资源
最近更新 更多