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.