ios - 自定义对象返回 NSDictionary 作为类型

标签 ios objective-c xcode object

我的对象CCategory.h

    @interface CCategory : NSObject

@property(strong, nonatomic) NSNumber * _Nonnull categoryId;
@property(strong, nonatomic) NSNumber * _Nonnull originalId;
@property(strong, nonatomic) NSString * _Nonnull name;
@property(strong, nonatomic) NSString * _Nonnull type;
@property(nonatomic, strong) CCategory * _Nullable  parent;
@property (nullable, nonatomic, retain) NSOrderedSet<CCategory *> *children;



- (instancetype _Nonnull )initWithId:(NSNumber *_Nullable)categoryId
               andOriginalId:(NSNumber *_Nullable)originalId
                   andName:(NSString *_Nonnull)name
                   andType:(NSString *_Nonnull)type
                   andParent:(CCategory *_Nullable)parent
                   andChildren:(NSOrderedSet<CCategory *> *_Nullable)children NS_DESIGNATED_INITIALIZER;
@end

CCategory.m

@implementation CCategory


- (instancetype)init {
    return [self initWithId:0 andOriginalId:0 andName:@"" andType:@"" andParent:nil andChildren:nil];
}

- (instancetype)initWithId:(NSNumber *)categoryId
               andOriginalId:(NSNumber *)originalId
                   andName:(NSString *)name
                   andType:(NSString *)type
                    andParent:(CCategory *)parent
                   andChildren:(NSOrderedSet<CCategory *> *)children {

    self = [super init];
    if (self) {
        self.categoryId = categoryId;
        self.originalId = originalId;
        self.name = name;
        self.type = type;
        self.parent = parent;
        self.children = children;

    }
    return self;
}

@end

这是我检查类类型的方式:

        CCategory * firstItem = [itemsArray objectAtIndex:0];

        CCategory *child = [firstItem.children objectAtIndex:0];

        NSString *className = NSStringFromClass([child class]);

        NSLog(@"First Item is: %@", className);

firstItem 返回类型CCategory,child 返回类型NSDictionary

从数据库接收对象包含所有数据后,但children由于某种原因是NSDictionary类型,而不是CCategory类类型。这是为什么?以及如何让 child 输入 CCategory

最佳答案

因为您声明了某个类的某个对象并不意味着它属于正确的类。 例如,如果你写

NSArray *array = [@[@"Hello"] firstObject]; 

array实际上是一个 NSString对象。

因此,当您解析您的响应并创建您的 CCategory 时我猜的对象 NSDictionary对象。
这就是为什么 children似乎实际上是一个 NSOrderedSetNSDictionary而不是 CCategory对象。

一种可能的方法是递归调用 initWithId:andOriginalId:andName:andType:andParent:andChildren:为了 children 。

所以不是 self.children = children;

NSMutableOrderedSet *childrenSet = [[NSMutableOrderedSet alloc] init]; 
for (NSDictionary *aChildDict in [children array])
{
    CCategory *aChild = [CCategory alloc] initWithId:aChildDict[keyWhereThereIsID], etc.] 
    [childrenSet addObject:aChild];
}
self.children = childrenSet;

但是像在 init 中那样设置它更像是一种技巧。方法,因为它说 children应该是 NSOrderedSet<CCategory *> * .

所以由您决定,要么重命名该方法以明确其作用,要么接受 NSOrderedSet<NSDictionary *> *对于 children相反,先解析它,再创建一个,等等。

一个可能的懒惰选择是这样做:

重命名为 andChildren:(NSOrderedSet *)children

NSMutableOrderedSet *childrenSet = [[NSMutableOrderedSet alloc] init]; 
for (id *aChildObject in [children array])
{
    CCategory *aChild = nil;
    if ([aChildObject isKindOfClass:[NSDictionary class]]) //Need parsing
    {
        NSDictionary *aChildDict = (NSDictionary *)aChildObject;
        aChild = [CCategory alloc] initWithId:aChildDict[keyWhereThereIsID], etc.];
    }
    else if ([aChildObject isKindOfClass:[CCategory class]]) //Already a CCategory Object
    {
        aChild = (CCategory  *)aChildObject;
    }
    else 
    {
        NSLog(@"Ooops, child of wrong class: %@", NSStringFromClass([aChildObject class]);
    }

    if (aChild) { [childrenSet addObject:aChild]; }
}
self.children = childrenSet;

关于ios - 自定义对象返回 NSDictionary 作为类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44543412/

相关文章:

ios - 如何取消正在进行的NSArray排序?

iphone - 将我的 iphone 应用程序添加为设备中的内容菜单

ios - 创建一个搜索栏,就像在 iOS 7 的联系人应用中一样

ios - 如何将不可见的边界框添加到 SKSpriteNode 以更好地检测 touchEvent?

objective-c - 管理 NSSearchfield 上的键盘事件,无需子类化

xcode - SWIFT + 解析 signUpInBackgroundWithBlock 不再有效 : Xcode 6. 3.1

ios - 在 iPad 上模拟 sleep 模式

iOS [[MPMediaQuery playlistsQuery] collections] 在第一次运行时返回 "Playback History"

objective-c - Xcode/Cocoapods 为什么我不能从 Pod 实现 Swift 协议(protocol)?

objective-c - 在 Realm 中添加属性,导致项目崩溃