簡體   English   中英

iOS:檢查值? NSDictionary,NSArray,NSString

[英]iOS: Check values? NSDictionary, NSArray, NSString


跳過以下

這些值是TVC didSelectRowAtIndexPath方法的前三行,當我連續選擇一個項目時,該方法將給我帶來各種異常。

我如何:

一種。 檢查下面的字典,數組和selectedCategory是否具有值。

b。 確保事實證明他們不這樣做嗎?

NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"News"];
NSString *selectedCategory = [array objectAtIndex:indexPath.row];

到這里 :

UIView tableView:numberOfRowsInSection:]中的異常:無法識別的選擇器發送到實例0x7193a60

看來NSPlaceholderString的initWithString方法得到了nil作為參數。 因此,要弄清楚何時恰好發生這種情況,最好添加一個異常斷點。 在這里看看

要檢查它們是否具有值,可以檢查...

[dictionary count];

[array count];

[selectedCategory length];

或者您可以檢查諸如...

dictionary == nil;

array == nil;

[selectedCategory isEqualToString:@""];

selectedCategory == nil;

有很多方法。 不確定您要問的是什么,但是您可以檢查這些。

這是一個簡單的調整。 通常,有比嵌套所有這些數據集合更好的存儲數據的方法,但是,這只是替換已經存在的應該停止崩潰的內容而已。

// First check your listOfItems
NSDictionary *dictionary = NULL;
if (listOfItems.count > indexPath.section) {
    dictionary = [listOfItems objectAtIndex:indexPath.section];
}

// Next check your dictionary isn't null.
NSArray *array = NULL;
if (dictionary) {
    array = [dictionary objectForKey:@"News"];
}

// Next check your array and make sure your array has values.
NSString *selectedCategory = NULL;
if (array.count > indexPath.row) {
   selectedCategory = [array objectAtIndex:indexPath.row];
}

// Finally do your stuff
if (selectedCategory) {
    // Do your stuff
} else {
    // Something went wrong along the way and you don't have a selectedCategory string.
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM