简体   繁体   中英

Creating an Array from NSObject Class

I have an NSObject class call Details that holds 3 properties.

 @interface Details : NSObject

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

- (NSDictionary *_Nonnull)getMappedDictionary; @end

And the impelentation of this class is

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

In another class call Request Class i would like to create an array to hold Details class's properties (image, number, 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;
}

But i can not able to have this class's properties as an array... What am i missing here? Any help would be perfect! Thanks

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

means that you create a dictionary with nil. objects[0] means your fist object is nil. so I guess that when you create a dictionary by this.

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

ObjectOrNull([self getImageBase64]) return a nil.

From the comments it seems the issue is in fact trying to inset a nil object into dictionary:

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

The code that is provided would point to getMappedActionDetails which is most likely inserting a nil object. Assuming ObjectOrNull does what it implies I would wager that in one of your models _details property is nil . Maybe the fix is:

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

I also suggest using exception breakpoints to get the exact location of the crash.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM