我試圖在我的if語句中遇到問題。我取從核心數據DB qDiff(位整數),一切工作,除了得到一擊的if語句:if語句沒有命中
NSString *diff;
NSArray *fetchedObjects = [qContext executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *info in fetchedObjects) {
NSLog(@"idQ: %@", [info valueForKey:@"idQ"]);
NSLog(@"qDiff: %@", [info valueForKey:@"qDiff"]); //<<<<<<<<<Print correct number
diff = [info valueForKey:@"qDiff"];
NSLog(@"diff: %@", diff); //<<<<<<<<<Print correct number
if ([diff isEqual:@"1"]) NSLog(@"EASY");
//if ([diff isEqualToString: @"2"]) NSLog(@"MEDIUM");
//if ([[info valueForKey:@"qDiff"] isEqualToString: @"3"]) NSLog(@"HARD"); //<<<<<<tried this first
NSLog(@"question: %@", [info valueForKey:@"question"]);
}
[fetchRequest release];
我一直在嘗試不同的if語句,但它不會觸發(打印NSLog的)。
任何人都可以給我一個提示?
========= UPDATE ==========
我改成:
if ([diff isEqualToString:@"1"]) NSLog(@"EASY");
,得到了以下幾點:
2010-12-12 15:39:49.321 XX_v2 [2950:207] IDQ:0
2010-12-12 15:39:49.321 XX_v2 [2950:207] qDiff:1
2010-12-12 15 :39:49.3 22_x_v2 [2950:207] diff:1
2010-12-12 15:39:49.322 XX_v2 [2950:207] - [_ PFCachedNumber isEqualToString:]:無法識別的選擇器發送到實例0xa0f9008
2010-12-12 15: 39:49.324 XX_v2 [2950:207] *終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因: ' - [_ PFCachedNumber isEqualToString:]:無法識別的選擇發送到實例0xa0f9008'
==== ====== UPDATE#2 =======
Did try:
if ([diff isEqual:@"1"]) NSLog(@"EASY");
在輸出沒有差別:
2010-12-12 15:55:57.381 XX_v2 [3151:207] IDQ:0
2010-12-12 15:55:57.382 XX_v2 [ 3151:207] qDiff:1
2010-12-12 15:55:57.382 XX_v2 [3151:207] diff:1
2010-12-12 15:55:57.382 XX_v2 [3151:207]問題:你想要一個新的?
然而,在調試器我發現:
'差異' 「變量不是CFString字符串」
========更新#3 === =====
更新了上面的代碼以顯示'diff'的定義。
========更新#4 ========
我也終於有這種說法解決問題:
if ([[info valueForKey:@"qDiff"] intValue] == 1) NSLog(@"EASY");
你在哪裏聲明'diff'? – 2010-12-12 15:04:12