我正在尝试将我的 NSDictionary 分配给我的 NSArray 但每次应用程序崩溃都是我的代码:
NSDictionary *dict = [[NSDictionary alloc]init];
for (int i=0; i< myMutableArray.count; i++) {
dict = [NSDictionary dictionaryWithObjectsAndKeys:[[myMutableArray objectAtIndex:i]xmlhotel_name], @"hotelname",
[PriceArray objectAtIndex:i], @"startingfrom",
[[myMutableArray objectAtIndex:i]xmlhotel_city],@"city",
[DistanceArray objectAtIndex:i],@"distance",
[[myMutableArray objectAtIndex:i]xmlhotel_image],@"imagesnames",
[[myMutableArray objectAtIndex:i]xmlhotel_stars],@"numberofstars",
@"45.5016889",@"hotelLat",
@"-73.567256",@"hotelLong", nil];
}
NSArray *myArray = @[@{[dict allKeys] : [dict allValues]}];
这是我传递数据的时候
BookATableController = [self.storyboard instantiateViewControllerWithIdentifier:@"bookatable"];
BookATableController.myMutableArray=[[NSMutableArray alloc]initWithArray:self.rssOutputData];/// Passing Data
BookATableController.PriceArray=[[NSMutableArray alloc]initWithArray:rssHotelPrice];/// Passing Data
BookATableController.DistanceArray=[[NSMutableArray alloc]initWithArray:rssHotelDistance];/// Passing
[self.view addSubview:BookATableController.view];
[self addChildViewController:BookATableController];
[BookATableController didMoveToParentViewController:self];
最佳答案
一个字典不起作用,因为当前状态下的键不是唯一的。可能的解决方案的两个建议,
1) 我认为最好的方法是创建一个 Hotel 对象,其中包含您想要存储的属性并在 for 循环中初始化这些对象。如果您愿意,我可以给您举一个例子。
2) 如果你想使用字典,你可以像我在下面列出的那样做,尽管你可能还可以尝试许多其他变体。
NSMutableArray *arrayOfHotels = [[NSMutableArray alloc] init];
// 5 is random, you can use the count from 'myMutableArray'
for (int i=0; i < 5; i++) {
NSArray *columnNames = @[@"hotelname",
@"startingfrom",
@"city",
@"distance",
@"imagesnames",
@"numberofstars",
@"hotelLat",
@"hotelLong"];
NSArray *values = @[[[myMutableArray objectAtIndex:i]xmlhotel_name],
[PriceArray objectAtIndex:i],
[[myMutableArray objectAtIndex:i]xmlhotel_city],
[DistanceArray objectAtIndex:i],
[[myMutableArray objectAtIndex:i]xmlhotel_image],
[[myMutableArray objectAtIndex:i]xmlhotel_stars],
@"45.5016889",
@"-73.567256"];
[arrayOfHotels addObject:[NSDictionary dictionaryWithObjects:columnNames forKeys:values]];
}
关于ios - 尝试将 nsdictionary 分配给我的 nsarray 但应用程序每次都会崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39014608/