ios - 尝试将 nsdictionary 分配给我的 nsarray 但应用​​程序每次都会崩溃

标签 ios objective-c nsmutablearray nsarray nsdictionary

我正在尝试将我的 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/

相关文章:

ios - 解析检索指针对象

objective-c - 如何使用自定义参数从我的应用程序启动应用程序,以便我可以检查该应用程序是否由我启动?

objective-c - 带有 NSMutableArray 字符串的 iPhone EXC_BAD_ACCESS

ios - 信任 iOS 13 上基于 TLS 的 WebSocket 的自签名证书

ios - 无法更改 UINavigationBar 颜色(快速)

ios - 如何创建地址簿和联系人

iphone - UIWebView 加载数据和 iWork 文件

objective-c - 当条目更新但未保存时,核心数据会重复

ios - [__NSArrayM objectForKeyedSubscript :]: unrecognized selector sent to instance - source code and screenshot attached

ios - 空的 UITableView