我無法添加對象到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