2015-04-28 51 views
-2

我想將字典轉換爲Objective-C中的數組。我在第三行得到這個警告。 '不兼容的指針類型將nsdictionary賦值給nsarray類型的參數'。有沒有辦法將該字典轉換爲數組而不會收到警告?不兼容的指針類型NSDictionary分配NSArray類型的參數

// in DataSource.h 

@property (nonatomic,strong) NSArray *bars; 

// in MasterViewController.m 

     AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
operation.responseSerializer = [AFJSONResponseSerializer serializer]; 
operation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"]; 

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 


    NSDictionary *JSONDict = (NSDictionary *) responseObject; 
    _dataSource.recievedJSON = JSONDict; 
    _dataSource.bars = [NSArray arrayWithArray:JSONDict]; 

    self.title = @"Bars"; 
    [self.tableView reloadData]; 

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 

    // display alert if error downlading data 
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather" 
                 message:[error localizedDescription] 
                 delegate:nil 
               cancelButtonTitle:@"Ok" 
               otherButtonTitles:nil]; 
    [alertView show]; 
    }]; 

    [operation start]; 
+0

尋求調試幫助的問題(「爲什麼這個代碼不工作?」)必須包含所需的行爲,特定的問題或錯誤以及在問題本身中重現問題所需的最短代碼。 –

+0

不知道如何聲明'酒吧'只能回答「是」,沒有給你任何進一步的建議。 –

+0

NSDictionary不是NSArray,那麼爲什麼你會嘗試將它作爲參數傳遞給'arrayWithArray:'?這是堅果。 – matt

回答

1

你是在說:

[NSArray arrayWithArray:JSONDict] 

JSONDict是一個NSDictionary,而arrayWithArray:期待一個NSArray。那你爲什麼這樣做?

您需要考慮如何將字典轉換爲數組 - 如果這是您的目標 - 然後需要這樣做。你需要理解字典的結構,並考慮這可能會變成什麼樣的「數組」。

+0

但是,爲什麼你想把字典變成一個數組完全神祕,所以我不能說更多。你爲什麼不把它存儲成它是一本字典? – matt

0

你不能將NSDictionary(鍵值對)轉換爲NSArray,但如果你想獲得一個字典的鍵和值的數組,所以可以試試這個。

NSDictionary* dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"object1",@"key1",@"object2",@"key2",@"object3",@"key3", nil]; 
NSArray* arrKeys = dic.allKeys; 
NSArray* arrValues = dic.allValues; 
相關問題