尝试指派的NSDictionary我的NSArray,但应用程序崩溃每次

问题描述:

我想我的NSDictionary分配给我的NSArray,但每次在这里的应用程序崩溃是我的代码:尝试指派的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]; 
+0

什么是崩溃? – Avi

+0

我尝试了一个简化的例子,并没有崩溃。 – Avi

+0

***终止应用程序由于未捕获的异常“NSInvalidArgumentException”,原因是:“ - [NSNull长]:无法识别的选择发送到实例0x19f0e4490” –

一本词典是行不通的,因为你在当前状态下键是不是唯一的。可能的解决办法两条建议,

1)我认为最好是建立一个酒店的对象与要存储在您的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]]; 
    } 
+0

所以如何我可以使用arrayOfHotels作为NSArray吗? –

+0

arrayOfHotels是一个字典数组,因此[arrayOfHotels objectAtIndex:0]将返回一个字典,其中包含您酒店的所有详细信息。您希望如何使用结果? – JingJingTao

+0

我得到***终止应用程序由于未捕获的异常'NSInvalidArgumentException',原因:' - [NSNull长度]:无法识别的选择器发送到实例0x19f0e4490' –