gpt4 book ai didi

ios - 从 NSObject 类创建数组

转载 作者:行者123 更新时间:2023-11-28 23:57:37 25 4
gpt4 key购买 nike

我有一个包含 3 个属性的 NSObject 类调用 Details。

 @interface Details : NSObject

@property (nonatomic, nonnull, strong) UIImage *image;
@property (nonatomic, assign) NSInteger number;
@property (nonatomic, nonnull, strong) NSString *details;

- (NSDictionary *_Nonnull)getMappedDictionary; @end

这个类的实现是

@interface Details()
@property (nonatomic, nonnull) NSString *imageFormat;
@property (nonatomic, nonnull) NSData *imageData;
@end

@implementation Details

- (instancetype)init {
if (self = [super init]) {
_imageFormat = @"jpg";
}
return self;
}

- (NSData *)imageData {
if (!_imageData) {
_imageData = UIImageJPEGRepresentation(self.image, 1.0);
}
return _imageData;
}

- (NSInteger)number {
return _number;
}

- (NSString *)details {
return _details;
}

- (NSString *)getImageBase64 {
NSString *base64String = @"";
base64String = [self.imageData base64EncodedStringWithOptions:kNilOptions];
return base64String;
}

static id ObjectOrNull(id object) {
return object ?: [NSNull null];
}

- (NSDictionary *)getMappedDictionary {
return @{ImageKey : ObjectOrNull([self getImageBase64]), NumberKey : @(_number), DetailKey : _details};
}

在另一个调用 Request Class 的类中,我想创建一个数组来保存 Details 类的属性(图像、数字、详细信息)

- (NSMutableSet<Details *> *)details {
if (!_details) {
_details = [NSMutableSet new];
}
return _dDetails;
}

- (NSArray *)getMappedActionDetails {
NSMutableArray *details = [NSMutableArray new];
for (Details *detail in self.details) {
[details addObject:[detail getMappedDictionary]];
}
return details;
}

但我无法将此类的属性作为数组...我在这里缺少什么?任何帮助都是完美的!谢谢

最佳答案

 -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]'

意味着你用 nil 创建了一个字典。objects[0] 意味着你的拳头对象是零。所以我想当你用这个创建字典时。

- (NSDictionary *)getMappedDictionary {
return @{ImageKey : ObjectOrNull([self getImageBase64]), NumberKey : @(_number), DetailKey : _details};
}

ObjectOrNull([self getImageBase64]) 返回 nil。

关于ios - 从 NSObject 类创建数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50715338/

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