2010-07-22 37 views
3

我已經制定了下面的代碼:NSPredicate - 工作不正常

NSString *mapIDx = @"98"; 
    NSLog(@"map id: %@", mapIDx); 

    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"WayPoint" inManagedObjectContext:managedObjectContext]; 
    [request setEntity:entity]; 

    //NSPredicate *predicate = [NSPredicate predicateWithFormat:@"waypoint_map_id=%@", mapIDx]; 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"waypoint_map_id==%@", mapIDx]; 
    [request setPredicate:predicate]; 

    NSError *error; 
    listArray = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy]; 
    [request release]; 


    int arrayItemQuantity = [listArray count]; 
    NSLog(@"Array Quantity: %d", arrayItemQuantity); 

    // Loop through the array and display the contents. 
    int i; 
    for (i = 0; i < arrayItemQuantity; i++) 
    { 
     NSLog (@"Element %i = %@", i, [listArray objectAtIndex: i]); 
    } 

    /* 
    NSInteger *xCoordinate = listArray[1]; 
    NSInteger *yCoordinate = listArray[3]; 
    NSLog(@"xCoordinate: %@", xCoordinate); 
    NSLog(@"yCoordinate: %@", yCoordinate); 

    CLLocationCoordinate2D coordinate = {xCoordinate, yCoordinate}; 
    MapPin *pin = [[MapPin alloc]initwithCoordinates:coordinate]; 
    [self.mapView addAnnotation:pin]; 
    [pin release]; 
    */ 

    [listArray release]; 

正如你可以看到,我想從我的數據庫,與98 waypoint_map_id什麼選擇特定的對象,但NSPredicate沒有按照我的預期工作。零對象正在被選中。

任何任何想法?

回答

6

帶格式的謂詞不會將字符串「98」轉換爲數字。取而代之的是它

waypoint_map_id == "98" 

...這是尋找字符串屬性。謂詞更改爲:

NSInteger mapIdx=98; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"waypoint_map_id==%d", mapIDx]; 

...返回的斷言:解決

waypoint_map_id == 98 
+0

謝謝,waypoint_map_id是數據庫中的一個字符串。 – Stephen 2010-07-22 15:21:46

+0

哦,那你確定你有一個'WayPoint'對象,其'waypoint_map_id'屬性是「98」嗎?您可以嘗試註釋謂詞並轉儲所有要檢查的「WapPoint」對象。 – TechZen 2010-07-22 17:31:24

+0

另外,註釋掉的代碼看起來很奇怪。返回數組中的第一個和第三個元素將是'WayPoint' NSManagedObjects(或一個子類)。由於您沒有分配給抓取的排序描述符,它們將以隨機順序排列。然後,你將他們的地址指定爲整數的指針,然後嘗試在座標定義中將這些地址用作雙精度。這是行不通的。我認爲你可能對你的對象有些困惑。 – TechZen 2010-07-22 17:32:35

2

假設您確實在數據庫中有該對象,請嘗試圍繞該值添加引號?

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"waypoint_map_id==\"%@\"", mapIDx]; 

(在救命稻草!)

你prediacte看起來不錯,所以我會立即開始懷疑該bug是在別處:

  • definintely是否有與該ID的航點?
  • 是listArray nil,即其他內容在請求中出錯了嗎?

你不檢查是什麼錯誤 - 也許這會給你更多的信息?

NSError *error = nil; 
NSArray *results = [managedObjectContext executeFetchRequest:request error:&error]; 
[request release]; 

if (nil == results || nil != error) 
    NSLog(@"Error getting results : %@", error); 

listArray = [results mutableCopy]; 

希望這對您有所幫助!

+0

感謝,我已經把錯誤校驗碼並重新測試,沒有得到任何錯誤。有確定的航點與該ID,當我運行沒有謂詞,我可以在控制檯中看到輸出。 – Stephen 2010-07-22 15:23:10

+0

如果你運行它沒有prediacte和輸出__NSLog(@「%@」,item.waypoint_map_id); __ listArray中的每個項目,你會得到什麼? – deanWombourne 2010-07-22 15:59:13

+0

以下行:NSLog(@「Element%i =%@」,i,[[listArray objectAtIndex:i] valueForKey:@「waypoint_map_id」]);將元素1813 = 98'輸出到控制檯。 – Stephen 2010-07-22 16:06:27

1

問題:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"waypoint_map_id contains[cd] %@", mapIDx]; 
    [request setPredicate:predicate];