2016-08-18 66 views
0

我想我的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

一本詞典是行不通的,因爲你在當前狀態下鍵是不是唯一的。可能的解決辦法兩條建議,

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' –

相關問題