0

重複の可能性:
Objective-CでのNSString比較の理解

ヘッダーファイル:

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

保存されたCoredataNSString:

[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モデルも文字列型です。

4

1 に答える 1

2

コードでは、オブジェクトを比較していますが、文字列を比較する場合は、次のようにする必要があります。

if ([preference.pid isEqualToString:@"0"])
于 2013-01-04T21:45:07.460 に答える