У меня возникли проблемы с добавлением объектов в NSMutableArray. Я четко размещаю 2 объекта в массиве typeList, но счет отображается только как 1. Что я делаю неправильно?Создание NSMutableArray объектов
content.h
@interface TBContentModel : NSObject
+(NSMutableArray*)typeList;
+(void)setTypeList:(NSMutableArray*)str;
content.m
static NSMutableArray *typeList = nil;
@implementation TBContentModel
- (id) init {
self = [super init];
if (self) {
typeList = [NSMutableArray array];
}
return self;
}
contentviewcontroller.m
@implementation TBViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *jsonString = @"[{\"Content\":268,\"type\":\"text\"},{\"Content\":65,\"type\":\"number\"}]";
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
for (NSMutableDictionary *dictionary in array)
{
TBContentModel *test = [[TBContentModel alloc] init];
test.type = dictionary[@"type"];
[[TBContentModel typeList] addObject:test];
NSLog(@"%@", test.type);
}
}
- (IBAction)tapButton:(id)sender {
NSLog(@"%d", [TBContentModel.typeList count]); // always shows 1
}
Пожалуйста, сократить количество кода в вашем примере и выделить, где проблема. – trojanfoe
вырезать код. Я новичок, поэтому не был уверен, какой код был связан с проблемой. – Sancho