Iphone NSMutableArray似乎提前发布
我正在尝试向数组添加注释,以便在地图上放置多个管脚。我把一切都安排得井井有条。第一次循环时,它将对象添加到数组中。当它通过。。。数组中有0个对象。谁能告诉我为什么 编辑:我正在使用ARCIphone NSMutableArray似乎提前发布,iphone,objective-c,ios,nsmutablearray,Iphone,Objective C,Ios,Nsmutablearray,我正在尝试向数组添加注释,以便在地图上放置多个管脚。我把一切都安排得井井有条。第一次循环时,它将对象添加到数组中。当它通过。。。数组中有0个对象。谁能告诉我为什么 编辑:我正在使用ARC -(void)plotMultipleLocs { float latitude; float longitude; NSRange commaIndex; NSString *coordGroups; for (int i=0; i<=cgIdArray.count; i++) { coordG
-(void)plotMultipleLocs {
float latitude;
float longitude;
NSRange commaIndex;
NSString *coordGroups;
for (int i=0; i<=cgIdArray.count; i++) {
coordGroups = [cgInAreaArray objectAtIndex:i];
commaIndex = [coordGroups rangeOfString:@","];
latitude = [[coordGroups substringToIndex:commaIndex.location] floatValue];
longitude = [[coordGroups substringFromIndex:commaIndex.location + 1] floatValue];
CLLocationCoordinate2D loc = CLLocationCoordinate2DMake(latitude, longitude);
MKCoordinateRegion reg = MKCoordinateRegionMakeWithDistance(loc, 1000, 1000);
self->mapView.region = reg;
MKPointAnnotation* ann = [[MKPointAnnotation alloc] init];
ann.coordinate = loc;
ann.title = [cgNameArray objectAtIndex:i];
ann.subtitle = [cgLocArray objectAtIndex:i];
NSMutableArray *mutAnnArray = [NSMutableArray arrayWithArray:annArray];
[mutAnnArray addObject:ann];
}
}
-(无效)绘图多语言{
浮动纬度;
浮动经度;
NSRange commaIndex;
NSString*合作组;
对于(int i=0;imapView.region=reg;
MKPointAnnotation*ann=[[MKPointAnnotation alloc]init];
ann.coordinate=loc;
ann.title=[cgNameArray objectAtIndex:i];
ann.subtitle=[cgLocArray objectAtIndex:i];
NSMUTABLEARRY*mutanarray=[NSMUTABLEARRY arrayWithArray:ANARRAY];
[mutanarray addObject:ann];
}
}
您是否尝试保留实例以避免其被释放?您是否尝试保留实例以避免其被释放?您正在循环中创建一个可变数组并将对象添加到其中
在循环的下一次迭代中,您将创建一个新的可变数组并向其添加一个新注释
请忽略以下事实:您是从另一个数组创建它的,而不仅仅是将注释添加到annArray
基本上,要添加对象的数组将持续一次迭代,然后超出范围
尝试将阵列移出循环:
-(void)plotMultipleLocs {
float latitude;
float longitude;
NSRange commaIndex;
NSString *coordGroups;
NSMutableArray *mutAnnArray = [NSMutableArray arrayWithArray:annArray]; // Create one array outside the loop.
for (int i=0; i<=cgIdArray.count; i++) {
coordGroups = [cgInAreaArray objectAtIndex:i];
commaIndex = [coordGroups rangeOfString:@","];
latitude = [[coordGroups substringToIndex:commaIndex.location] floatValue];
longitude = [[coordGroups substringFromIndex:commaIndex.location + 1] floatValue];
CLLocationCoordinate2D loc = CLLocationCoordinate2DMake(latitude, longitude);
MKCoordinateRegion reg = MKCoordinateRegionMakeWithDistance(loc, 1000, 1000);
self->mapView.region = reg;
MKPointAnnotation* ann = [[MKPointAnnotation alloc] init];
ann.coordinate = loc;
ann.title = [cgNameArray objectAtIndex:i];
ann.subtitle = [cgLocArray objectAtIndex:i];
[mutAnnArray addObject:ann]; // Add the annotation to the single array.
}
// mutAnnArray will go out of scope here, so maybe return it, or assign it to a property
}
-(无效)绘图多语言{
浮动纬度;
浮动经度;
NSRange commaIndex;
NSString*合作组;
NSMutableArray*mutanarray=[NSMutableArray arrayWithArray:annArray];//在循环外创建一个数组。
对于(int i=0;imapView.region=reg;
MKPointAnnotation*ann=[[MKPointAnnotation alloc]init];
ann.coordinate=loc;
ann.title=[cgNameArray objectAtIndex:i];
ann.subtitle=[cgLocArray objectAtIndex:i];
[mutanarray addObject:ann];//将注释添加到单个数组中。
}
//mutanarray将超出此范围,因此可能会将其返回,或将其分配给属性
}
您正在循环中创建一个可变数组,并将对象添加到其中
在循环的下一次迭代中,您将创建一个新的可变数组并向其添加一个新注释
请忽略以下事实:您是从另一个数组创建它的,而不仅仅是将注释添加到annArray
基本上,要添加对象的数组将持续一次迭代,然后超出范围
尝试将阵列移出循环:
-(void)plotMultipleLocs {
float latitude;
float longitude;
NSRange commaIndex;
NSString *coordGroups;
NSMutableArray *mutAnnArray = [NSMutableArray arrayWithArray:annArray]; // Create one array outside the loop.
for (int i=0; i<=cgIdArray.count; i++) {
coordGroups = [cgInAreaArray objectAtIndex:i];
commaIndex = [coordGroups rangeOfString:@","];
latitude = [[coordGroups substringToIndex:commaIndex.location] floatValue];
longitude = [[coordGroups substringFromIndex:commaIndex.location + 1] floatValue];
CLLocationCoordinate2D loc = CLLocationCoordinate2DMake(latitude, longitude);
MKCoordinateRegion reg = MKCoordinateRegionMakeWithDistance(loc, 1000, 1000);
self->mapView.region = reg;
MKPointAnnotation* ann = [[MKPointAnnotation alloc] init];
ann.coordinate = loc;
ann.title = [cgNameArray objectAtIndex:i];
ann.subtitle = [cgLocArray objectAtIndex:i];
[mutAnnArray addObject:ann]; // Add the annotation to the single array.
}
// mutAnnArray will go out of scope here, so maybe return it, or assign it to a property
}
-(无效)绘图多语言{
浮动纬度;
浮动经度;
NSRange commaIndex;
NSString*合作组;
NSMutableArray*mutanarray=[NSMutableArray arrayWithArray:annArray];//在循环外创建一个数组。
对于(int i=0;imapView.region=reg;
MKPointAnnotation*ann=[[MKPointAnnotation alloc]init];
ann.coordinate=loc;
ann.title=[cgNameArray objectAtIndex:i];
ann.subtitle=[cgLocArray objectAtIndex:i];
[mutanarray addObject:ann];//将注释添加到单个数组中。
}
//mutanarray将超出此范围,因此可能会将其返回,或将其分配给属性
}
每次通过循环时,您都会使用不同数组的内容创建一个新的可变数组。包含您在上一次迭代中添加的对象的可变数组不会保留。每次通过循环时,您都会使用不同数组的内容创建一个新的可变数组。包含您创建的对象的可变数组在上一次迭代中添加的内容不会被保留。我在使用ARC。如果我保留它,它会对我大喊大叫。Lol。我在使用ARC。如果我保留它,它会对我大喊大叫。Lol。是的……我的同事在我刷新页面之前就发现了。哈哈。谢谢你的回答。我会为你标记它,让你得到代表。我正在从annArray创建另一个数组,因为如果我将annArray声明为NSMutableArray,只是尝试使用它不起作用。我在数组中得到0个对象。annArray是一个ivar。是的……我的同事在我刷新页面之前就发现了。哈哈。谢谢你的回答。我会为你标记它,这样你就可以得到代表。我正在从annArray创建另一个数组,因为如果我声明ann数组作为NSMutableArray,只是尝试使用它不起作用。我在数组中得到0个对象。annArray是一个ivar。