将对象添加到实例变量数组

时间:2015-05-28 10:33:53

标签: ios arrays object save connection

有人可以帮我解决这个问题:

我在TableVC.m文件中创建了一个属性:

$var1 = result 1
$var2 = result 2
$var3 = result 3

在我的Viewdidload中,我实例化了数组:

@property NSMutableArray *savingBeaconSpecs;

现在我向服务器发出请求,我想将返回的JSON保存到对象中,并且每次都将它们保存在数组中。所以我在ConnectionDidFinishLaunching中执行了以下操作:

 NSMutableArray *savingBeaconSpecs = [[NSMutableArray alloc]init];

 self.artworkArray = [NSJSONSerialization JSONObjectWithData:self.data options:0 error:&err];
NSLog(@"Log ArtworkArray in ConnectionDidFinishLoading%@", self.artworkArray);

现在我想将此对象保存到savingBeaconSpecs NSMutableArray

Artwork *artwork = [[Artwork alloc]init];
artwork.title = [self.artworkArray valueForKey:@"name"];
artwork.artist = [[self.artworkArray objectForKey:@"artist"] valueForKey:@"name"];
artwork.CreationYear = [self.artworkArray valueForKey:@"creationYear"];
artwork.categorie = [[self.artworkArray objectForKey:@"exposition"] valueForKey:@"name"];

但是当我尝试记录他的内容时,NSMUtableArray savingBeaconSpecs总是返回0

有人请吗?

2 个答案:

答案 0 :(得分:1)

因为你在viewDidLoad中声明了它:

NSMutableArray *savingBeaconSpecs = [[NSMutableArray alloc]init];

你应该使用

 self.savingBeaconSpecs = [[NSMutableArray alloc]init];

[self.savingBeaconSpecs addObject:artwork];

并声明您的财产为(没有第一个首都S)

@property NSMutableArray *savingBeaconSpecs;

答案 1 :(得分:0)

要实例化数组,您应该: self.savingBeaconSpecs = [[NSMutableArray alloc] init];
或同样好: self.savingBeaconSpecs = [NSMutableArray array];