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

Trying to assign nsdictionary to my nsarray but app crashes everytime

我正在尝试将我的 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]];
   }