2013-01-04 16 views
0

可能重複:
Understanding NSString comparison in Objective-C如何比較coredata字符串和NSString的

頭文件

@property (nonatomic, strong) NSString *pid; 
@property (nonatomic, strong) NSString *name; 

Coredata的NSString存儲:

[newPref setValue: @"0" forKey:@"pid"]; //correctly show in DB & NSLog 
[newPref setValue: @"Sales" forKey:@"name"]; 

後取回後,評估失敗:

if(preference.pid == @"0") 

調試器說:

_pid = (NSSting *) 0x... @"o\xee\" 
_name = (NSString *))x0.. @<variable is not NSString> 

是我的NSString的存儲不正確,或者是我的評價錯了嗎? 注意:Coredata模型也是字符串類型。

+0

它很難說,如果你可能有一個錯誤在其他地方。但你通常應該比較字符串與isEqualToString: – Andy

+0

'頭文件:'你的意思是你的NSManagedObject類?它不應該包含@property值。 – TheJer

+0

http://stackoverflow.com/questions/3703554/understanding-nsstring-comparison-in-objective-c – Augie

回答

2

在代碼中,你比較對象,但如果你想比較字符串,它應該是

if ([preference.pid isEqualToString:@"0"])