2013-03-24 42 views
-2

我有這個代碼來獲取應用程序版本,並將其保存到nsdictionary獲取應用程序的版本是崩潰

NSString *Version=[NSString stringWithFormat:@"%@",[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]]; 

    NSLog(@"VERSION%@",Version); //prints the right thing 
    NSMutableDictionary *dic; 
    [dic setValue:Version forKey:@"version"]; //crash 
    [dic setValue:Errors forKey:@"errors"]; //work 

我得到崩潰的錯誤是:

setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key version 

你能幫助我找出這個錯誤?

非常感謝。

回答

0

我不得不分配詞典:

NSMutableDictionary *dic=[[NSMutableDictionary alloc]init]; 
0

你沒有alloc + init -ed字典dict

NSMutableDictionary *dic=[[NSMutableDictionary alloc] init]; 

這是必須的。

0

您不創建字典。由於它(大概)是一個局部變量,因此它保持未初始化的結果,其中包含未指定的值。就你而言,它指向的對象不是NSMutableDictionary。其實實例之一,它的工作:

NSMutableDictionary *dic = [NSMutableDictionary new]; 
0

你需要調用setObject:forKey:,不setValue:forKey:

NSString *Version=[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]; 

NSLog(@"VERSION = %@", Version); //prints the right thing 
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init]; 
[dic setObject:Version forKey:@"version"]; //crash 
[dic setObject:Errors forKey:@"errors"]; //work 

僅使用setValue:forKey:valueForKey:當你真正的意思是使用鍵 - 值編碼。否則請使用正確的setObject:forKey:objectForKey:

此外,請勿使用stringWithFormat:,除非您實際上有格式化字符串。

相關問題