2015-10-02 58 views
1

我的Nsdictionary看起來像這樣。訪問NSDictionary值目標C

{ 
thread = "{ 
\"uuid\": \"e9290344dee38c42782\", 
\"site_full\": \"www.independent.co.uk\", 
\"main_image\":\"http://www.independent.co.uk/incoming/article10498560.ece/binary/original/39-tea-party-ap.jpg\" 
}"; 
} 

[newDict valueForKeyPath:@"thread.main_image"]; 

我試圖訪問它這樣的,但它我可以得到所有這些都是線程之外的其他值,但不能在thread.Any幫助嵌套值表示讚賞。

EDIT

 for (int i=0;i < [responseObject count];i++) { 
       NSDictionary* newDict = (NSDictionary*)responseObject[i]; 

        if ([(NSString*)[newDict valueForKey:@"title"] isEqualToString: @""] || [newDict valueForKey:@"title"]== Nil) { 
         NSLog(@"Image or Title Text is null"); 
        } 
        else{ 
        NewsFeed* newsData = [NewsFeed new]; 
        newsData.newsSource = [newDict valueForKeyPath:@"thread.site"]; 
        newsData.newsImageLink = [newDict valueForKeyPath:@"thread.main_image"]; 
       //these three work fine 
        newsData.newsTitle = [newDict valueForKeyPath:@"title"]; 
        newsData.newsText = [newDict valueForKeyPath:@"text"]; 
        newsData.newsDate = [newDict valueForKeyPath:@"published"]; 
       // 
        NSLog(@"news Source :: %@ \n and news ImageLink:: %@ \n news Title %@ \nnews Text:: %@ \n news Date:: %@ ",newsData.newsSource,newsData.newsImageLink,newsData.newsTitle,newsData.newsText,newsData.newsDate); 
        [NewsData addObject:newsData]; 
        } 


     } 
+0

請顯示您嘗試過的代碼,以及它的缺陷。 –

+0

我已經添加了代碼,並且響應以數組的形式出現.. – soldiershin

回答

4

看起來像索引下標"thread"存儲的對象是而不是NSDictionary如假定的,而是它似乎是NSString的實例。

這是微妙的,但看的對象在根級辭典鍵"thread"打印的值:

This is an NSDictionary with one key: "thread" 
| 
|   This is an NSString object, see the quotes (")? 
|   | 
V   |      Notice that these other keys/values have their quotes (") escaped with backslash? 
{   V      | 
thread = "{     V 
\"uuid\": \"e9290344dee38c42782\", 
\"site_full\": \"www.independent.co.uk\", 
\"main_image\":\"http://www.independent.co.uk/incoming/article10498560.ece/binary/original/39-tea-party-ap.jpg\" 
}"; 
}^
    | 
    Here is the end of the NSString, again we see quotes (") 

假設這確實是問題,您需要將字符串轉換爲實際字典,您應該可以輕鬆地做到這一點using JSON serialization which this question/answer should help guide you to do.

一旦這個問題得到解決,您使用valueForKeyPath:應該是正確的,應該立即開始工作。

+1

剛剛檢查出來......確實是這個問題,並建議鏈接的解決方案正是我想要的..謝謝很多人.. – soldiershin

1

嘗試此現代目標-C代碼:

NSDictionary *myDict = @{@"thread" : @{@"uuid" : @"e9290344dee38c42782", @"site_full" : @"www.independent.co.uk", @"main_image" : @"http://www.independent.co.uk/incoming/article10498560.ece/binary/original/39-tea-party-ap.jpg"}}; 
NSLog(@"Image = %@", myDict[@"thread"][@"main_image"]); 

編輯: OP響應附加調試器屏幕截圖後:

enter image description here

+0

在debugginng po newDict [@「thread」]如果我這樣做,它列出所有的鍵和它的值,但是當我做這個po myDict [ @「thread」] [@「main_image」]它給出這個錯誤錯誤:警告:無法獲取cmd指針(替換NULL):在此框架中找不到名爲'_cmd'的變量 執行中斷,原因:試圖解除引用一個無效的ObjC對象或發送一個無法識別的選擇器。 該過程已經返回到表達式評估之前的狀態。 – soldiershin

+0

這對我來說很有魅力。從我的調試器查看附件截圖。我正在使用Xcode 7. – Abhinav

+2

正如Thuggish在上面指出的,看起來你沒有使用真正的字典,這就是爲什麼你無法看到它工作。如果您嘗試使用我的示例詞典,它應該可以工作。所以這裏的關鍵是檢查您的傳入數據並將其糾正爲正確的字典。 – Abhinav