2012-11-15 26 views
0

我有一個事件Entity,其操作爲Attribute。該操作是NSString,它會保存到iOS應用程序中每個事件的核心數據上。 event.action始終是一個預定義的字符串,例如..「Work」「Home」「Lunch」NSString的NSPredicate count個數

如何使用NSPredicate來檢索最後5個動作併爲我提供執行得最多的動作?

有使用NSPredicate的好教程嗎?

+0

我還在尋找解決辦法不要輕言放棄 – piam

回答

1

能否請您嘗試以下方法,希望在租賃指導你:)

NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" 
     inManagedObjectContext:managedObjectContext]; 
[request setEntity:entity]; 

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", @[@"Work", @"Home", @"Lunch"]]; //contain your actions 

[request setPredicate:predicate]; 
request.fetchLimit = 5;// retrieve the last 5 actions 
NSError *error; 
NSArray *yourActions = [managedObjectContext executeFetchRequest:request error:&error]; 

for (Event *event in yourActions) 
{ 
    // find the action that was perform the most in last 5 actions 
} 

請給我一個反饋,所以我知道的情況發生,所以我可以編輯代碼,以幫助您:) 。

感謝以下參考資料: Using Core Data with NSPredicate and NSDate Dynamically setting fetchLimit for NSFetchedResultsController http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Predicates/Articles/pUsing.html#//apple_ref/doc/uid/TP40001794-CJBDBHCB

+0

那指出我在正確的方向 –

+0

歡迎:) – piam