2013-10-07 85 views
1

我寫的一個方法如下面在CoreData中篩選?

-(NSArray*)getClubDetailedDeals:(NSString *)clubID{ 

    NSError *error; 
    NSManagedObjectContext *context = [self managedObjectContext]; 

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

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"ClubDetailsDeals" 
               inManagedObjectContext:context]; 

    [fetchRequest setEntity:entity]; 

    NSArray *fetchedArray1 = [[context executeFetchRequest:fetchRequest error:&error] mutableCopy]; 

    ClubDetailsDealsDAO *obb = [fetchedArray1 objectAtIndex:0]; 

    NSLog(@"MY ID IS %@ and count is %d", obb.dealname, [fetchedArray1 count]); 




    NSLog(@"deal is %@", clubID); 

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(%K CONTAINS %@)", @"clubid", clubID]; 

    //NSPredicate *predicate = [NSPredicate predicateWithFormat:@"dealid contains[cd] %@", dealID]; 

    [fetchRequest setPredicate:predicate]; 

    NSArray *fetchedArray = [[context executeFetchRequest:fetchRequest error:&error] mutableCopy]; 

    NSLog(@"COUNT of arary is ~~~~ %d", [fetchedArray count]); 

    return fetchedArray; 
} 

在這裏,陣列的在該日誌中的計數是

*的NSLog(@ 「MY ID IS%@ count是%d」,obb.dealname ,[fetchedArray1 count]); *

顯示了多個元素,因爲我添加了更多,它正常增加。

但是,當我使用此查詢

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(%K CONTAINS %@)", @"clubid", clubID]; 

它沒有返回取水的時候,連對象CoreData是可用的。

那麼什麼是正確的查詢來獲取匹配clubid,即NSString?

回答

0

嘗試此,

-(NSArray*)getClubDetailedDeals:(NSString *)clubID{ 

    NSError *error; 
    NSManagedObjectContext *context = [self managedObjectContext]; 

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

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"ClubDetailsDeals" 
               inManagedObjectContext:context]; 

    [fetchRequest setEntity:entity]; 

[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"(dealid 
contains[cd] %@)", clubID]]; 

    NSArray *fetchedArray = [[context executeFetchRequest:fetchRequest error:&error] mutableCopy]; 

    NSLog(@"COUNT of arary is ~~~~ %d", [fetchedArray count]); 

    return fetchedArray; 
} 
+0

[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@ 「(dealid 包含苯並[cd]%@)」,clubID]]; – Duaan

+0

是的,你在這裏弄錯了。你試過這個嗎? – karthika

+0

謝謝,它的工作原理,讓我知道因爲你有時間親愛的,我接受了答案:) – Duaan