从NSObject类创建数组

时间:2018-06-06 08:10:39

标签: ios nsarray nsdictionary nsobject nsmutableset

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

 @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;
}

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

2 个答案:

答案 0 :(得分:0)

从评论看来,问题似乎是试图将一个零对象插入字典:

  

&#39; *** - [__ NSPlaceholderDictionary initWithObjects:forKeys:count:]:   尝试从对象[0]

插入nil对象

提供的代码将指向最有可能插入getMappedActionDetails对象的nil。假设ObjectOrNull执行了它所暗示的内容,我会在您的某个模型中_details属性nil下注。也许修复是:

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

我还建议使用exception breakpoints来获取崩溃的确切位置。

答案 1 :(得分:0)

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

表示您使用nil创建字典。 对象[0]表示你的第一个对象是零。 所以当你用这个创建一个字典时我猜。

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

ObjectOrNull([self getImageBase64])返回零。

相关问题