gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:18:51 24 4
gpt4 key购买 nike

我的对象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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com