NSMutableArray 和 NSObject 问题
Posted
技术标签:
【中文标题】NSMutableArray 和 NSObject 问题【英文标题】:NSMutableArray and NSObject Issue 【发布时间】:2011-05-28 10:01:45 【问题描述】:Checkin *checkinsA = [[Checkin alloc] init];
NSDictionary *decodedJson = 结果; NSArray *users = [decodedJson objectForKey:@"data"]; Checkin *test = [[Checkin alloc] init];
for(NSDictionary *user in users)
NSLog(@"Created item: %@ \n", [user objectForKey:@"created_time"]);
checkinsA.time = [NSString stringWithFormat:@"%@",[user objectForKey:@"created_time"]];
NSDictionary *fromData = [user objectForKey:@"from"];
NSLog(@"user id is: %@ \n", [fromData objectForKey:@"id"]);
checkinsA.profID = [fromData objectForKey:@"id"];
NSLog(@"user name is: %@\n ", [fromData objectForKey:@"name"]);
checkinsA.name =[fromData objectForKey:@"name"];
NSDictionary *placeData = [user objectForKey:@"place"];
NSDictionary *locationData = [placeData objectForKey:@"location"];
NSLog(@"City: %@ \n", [locationData objectForKey:@"city"]);
checkinsA.city = [locationData objectForKey:@"city"];
NSLog(@"Country: %@ \n", [locationData objectForKey:@"country"]);
checkinsA.country = [locationData objectForKey:@"country"];
NSLog(@"Latitude: %@ \n", [locationData objectForKey:@"latitude"]);
checkinsA.lat = [locationData objectForKey:@"latitude"];
NSLog(@"Longitude: %@ \n", [locationData objectForKey:@"longitude"]);
checkinsA.lon = [locationData objectForKey:@"longitude"];
NSLog(@"Place name: %@ \n", [placeData objectForKey:@"name"]);
checkinsA.place = [placeData objectForKey:@"name"];
NSDictionary *tagData = [user objectForKey:@"tags"];
NSArray *tagDataArray = [tagData objectForKey:@"data"];
for(NSDictionary *tagData2 in tagDataArray)
NSLog(@"tagged user id is: %@ \n", [tagData2 objectForKey:@"id"]);
[checkinsA.taggedID addObject:[tagData2 objectForKey:@"id"]];
NSLog(@"tagged user name is: %@\n ", [tagData2 objectForKey:@"name"]);
[checkinsA.taggedName addObject:[tagData2 objectForKey:@"name"]];
[checkinArray addObject:checkinsA];
test = [checkinArray objectAtIndex:count2];
NSLog(@"Check array: %@",test.name);
count2++;
for(int i=0;i<count2;i++)
test = [checkinArray objectAtIndex:i];
NSLog(@"%@",test.name);
我的 NSobject 是 CheckinsA。我正在使用 NSDictionary 从响应字符串中收集数据。我在这里要做的是将每个 CheckinsA 保存在 checkinArray 中。这看起来不错:
`[checkinArray addObject:checkinsA];
test = [checkinArray objectAtIndex:count2];
NSLog(@"Check array: %@",test.name);`
并且正在打印正确的值。但是当循环完成并且我尝试使用循环打印数组中的值时,只打印出最后一个 CheckinsA!!我尝试用以下方式打印它:
test = [checkinArray objectAtIndex:0];
NSLog(@"%@",test.name);
test = [checkinArray objectAtIndex:1];
NSLog(@"%@",test.name);
(以防我的循环是问题,但它仍然只打印最后一个 CheckinsA)。 那么为什么我的整个数组只填充了最后一个 CheckinsA?好奇怪!
【问题讨论】:
【参考方案1】:您每次都将相同的对象添加到数组中。像这样移动代码:
Checkin *checkinsA
for(NSDictionary *user in users)
checkinsA = [[Checkin alloc] init];
// loop code
[checkinArray addObject:checkinsA];
[checkinsA release];
【讨论】:
感谢 vakio,但 Deepak 先回答,所以我必须将正确答案归功于他。【参考方案2】:每次循环时都需要初始化一个新对象。否则,您将更改同一个对象。
将Checkin *checkinsA = [[Checkin alloc] init];
设为循环中的第一行,在完成将checkinsA
添加到数组后的最后,执行[checkinsA release];
【讨论】:
这个怎么样?测试 = [checkinArray objectAtIndex:count2];?还是一样? 太棒了!!现在好了!只需等待 7 分钟即可将您的答案标记为正确的答案!!谢谢。【参考方案3】:通过检查您的数组 NSLog(@"%@",[checkinArray 描述]);
【讨论】:
以上是关于NSMutableArray 和 NSObject 问题的主要内容,如果未能解决你的问题,请参考以下文章
从 viewController 访问 NSObject 类中的 NSMutableArray
使用 NSPredicate 过滤 NSDictionary 的 NSMutableArray