2013-07-19 13 views
10

NSDictionaryが空であるかどうかを確認します。私はこれのようにやっている。NSDictionaryが空であることを確認してください。

mutDictValues = [[[NSUserDefaults standardUserDefaults] objectForKey:@"dicValues"]mutableCopy]; 
    NSLog(@"dictValues are %@",mutDictValues); 
    if(mutDictValues == NULL){ 
     arrCities = [[NSMutableArray alloc]init]; 
     NSLog(@"no cities seleceted"); 
    }else{ 
      arrCities = [[NSMutableArray alloc]init]; 
      arrCities = [mutDictValues objectForKey:@"cities"]; 
      [self placeCities]; 
    } 

しかし、それは次のエラーで、このラインarrCities = [mutDictValues objectForKey:@"cities"];上alwasyクラッシュ:

-[__NSCFConstantString objectForKey:]: 

誰かがこれで私を助けることができますか?

+0

場合([dictTemp isKindOfClass:[NSDictionaryのクラス]]!) – kb920

答えて

19

While retrieving the dictionary values from NSUserDefaults that dictionary automatically converted into string that is the reason for getting crashed and for checking dictionary use

[dictionary count]; 

EDIT: - 使用dictionaryForKey:方法

NSDictionary *dict =[[NSDictionary alloc]initWithObjectsAndKeys:@"hi",@"one",nil]; 
[[NSUserDefaults standardUserDefaults] setObject:dict forKey:@"dic"]; 
NSDictionary *dictn = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"dic"]; 
NSLog(@"%@",[dictn objectForKey:@"one"]); 
+0

ことが可能です{ //( "データが見つかりません" @)何か DisplayAlert を行います}これをNSDictionaryとして取得するには? – Steaphann

+0

これを一度確認してください。http://stackoverflow.com/questions/6797096/delete-all-keys-from-a-nsuserdefaults-dictionary-iphone – Balu

+0

@Stef Geelen:上記の回答の編集部分を確認してください。 – Balu

0
BOOL containsKeyABC = [myDict: valueForKey:@"ABC"]; 

int items = dict.count; 

if (items > 0) { 
    //not empty 
} 
2

あなたはNSDictionaryのようNSStringの(具体的なサブクラス)の治療どこか

if([myDict count] > 0) 
    NSLog(@"Dictionary is not empty"); 
else 
    NSLog(@"Dictionary is empty"); 
0

回答のほとんどは正確にあなたがそれゆえ例外を観察し、NSDictionaryの代わりにNSStringインスタンスに未認識セレクタobjectForKey:を渡していることを指摘したように

-[__NSCFConstantString objectForKey:]: 

は辞書などを返します。次の2つの方法

I. NSLogNSUserDefaults

NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]); 

II内のすべてのデータでこれを行うことができます。アプリケーションフォルダからNSUserDefaultsを格納しているplistファイルを確認します。詳細については、this answerを確認してください。

希望に役立ちます。

2
if ([mutDictValues count] == 0) { 
    //code here 
} 
else { 
    //code here 
} 

は、あなたのDICこれは

0

このコード

NSMutableDictionary *dict = ... 

BOOL isEmpty = ([dict count] == 0); 
1

を試みるを行う必要があります取得した後、私は少し別の問題があったが、私はここでそれを共有したいと思いますので、それが関係しています。

データを格納する&のWebサービスをNSDictionary &にフェッチしていましたが、objectForKeyをNullにフェッチしていました。だから、私が見つけた解決策はnsnullをを使用しての背後にある理由は、私は私が最終的にこの方法を考え出したアプリケーションをデバッグするとき、それは(nsnullを*)を返すあった

NSMutableDictionary *result = [NSJSONSerialization JSONObjectWithData:[str dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil]; 

    // NSLog(@"%@" , result); 

    NSMutableDictionary *sup = [result objectForKey:@"keysupplied"]; 
    NSNull *n=[NSNull null]; 
    if (sup ! = n){ 
     //Your code if its not null 
    } 

下の通りです。あなたは、コードの下に使用することができます

0

if(dict == nil) 
{ 
// blank 
}else{ 
// there is dictionary 
} 
関連する問題