2016-09-16 91 views
3

我在我的項目中使用一般的CoreData查詢方法。Swift3:將參數傳遞到NSFetchRequest方法

func query(table: String, searchPredicate: NSPredicate) -> [AnyObject] 
{ 
    let context = app.managedObjectContext 

    let fetchRequest = NSFetchRequest(entityName: table) 

    fetchRequest.predicate = searchPredicate 

    let results = try! context.fetch(fetchRequest) 
    return results 
} 

在Swift 3中這是行不通的。我發現這對蘋果公司的網站:

func findAnimals() 
{ 
    let request: NSFetchRequest<Animal> = Animal.fetchRequest 
    do 
    { 
     let searchResults = try context.fetch(request) 
     ... use(searchResults) ... 
    } 
    catch 
    { 
     print("Error with request: \(error)") 
    } 
} 

使用蘋果的例子,我怎麼會在通過動物的方法作爲參數,使findAnimals更通用的?

回答

3

我還沒有試過,但我認爲這樣的事情會工作...

func findCoreDataObjects<T: NSManagedObject>() -> [T] { 
    let request = T.fetchRequest 
    do 
    { 
     let searchResults = try context.fetch(request) 
     ... use(searchResults) ... 
    } 
    catch 
    { 
     print("Error with request: \(error)") 
    } 
} 

你必須使整個功能通用,所以你必須告訴它什麼類型T調用它時, 。

someObject.findCoreDataObjects<Animal>() 

認爲應該做的工作。不完全肯定,因爲我是新來的仿製藥自己:D

+0

其實如果我有機會,我張貼後花更多的時間與此:

import Foundation import Cocoa func addRecord<T: NSManagedObject>(_ type : T.Type) -> T { let entityName = T.description() let context = app.managedObjectContext let entity = NSEntityDescription.entity(forEntityName: entityName, in: context) let record = T(entity: entity!, insertInto: context) return record } func recordsInTable<T: NSManagedObject>(_ type : T.Type) -> Int { let recs = allRecords(T.self) return recs.count } func allRecords<T: NSManagedObject>(_ type : T.Type, sort: NSSortDescriptor? = nil) -> [T] { let context = app.managedObjectContext let request = T.fetchRequest() do { let results = try context.fetch(request) return results as! [T] } catch { print("Error with request: \(error)") return [] } } func query<T: NSManagedObject>(_ type : T.Type, search: NSPredicate?, sort: NSSortDescriptor? = nil, multiSort: [NSSortDescriptor]? = nil) -> [T] { let context = app.managedObjectContext let request = T.fetchRequest() if let predicate = search { request.predicate = predicate } if let sortDescriptors = multiSort { request.sortDescriptors = sortDescriptors } else if let sortDescriptor = sort { request.sortDescriptors = [sortDescriptor] } do { let results = try context.fetch(request) return results as! [T] } catch { print("Error with request: \(error)") return [] } } func deleteRecord(_ object: NSManagedObject) { let context = app.managedObjectContext context.delete(object) } func deleteRecords<T: NSManagedObject>(_ type : T.Type, search: NSPredicate? = nil) { let context = app.managedObjectContext let results = query(T.self, search: search) for record in results { context.delete(record) } } func saveDatabase() { let context = app.managedObjectContext do { try context.save() } catch { print("Error saving database: \(error)") } } 

與稱它爲我會意識到這是一直以來的正確答案。 – iphaaw

1

這個怎麼樣。

func query<T: NSManagedObject>(table: String, searchPredicate: NSPredicate) -> [T] { 
    let context = app.managedObjectContext 
    let fetchRequest: NSFetchRequest<T> = NSFetchRequest(entityName: table) 
    fetchRequest.predicate = searchPredicate 
    let results = try! context.fetch(fetchRequest) 
    return results 
} 
+0

已嘗試將此函數與func allRecords (表:String,sort:NSSortDescriptor?= nil) - > [T] { return query (table:table,search:nil,sort:sort ) } – iphaaw

+0

等什麼?你得到的賞金非常複製我的答案?這有什麼意義?大聲笑 – Fogmeister

+0

@Fogmeister - 我不得不長時間思考誰得到賞金,因爲答案是相似的。我可以將closetCoder的答案複製並粘貼到我的代碼中,這就是它的優點。儘管感謝您的幫助。 – iphaaw

0

這裏是最後的結果,可以幫助別人:

let name = "John Appleseed" 

let newContact = addRecord(Contact.self) 
newContact.contactNo = 1 
newContact.contactName = name 

let contacts = query(Contact.self, search: NSPredicate(format: "contactName == %@", name)) 
for contact in contacts 
{ 
    print ("Contact name = \(contact.contactName), no = \(contact.contactNo)") 
} 

deleteRecords(Contact.self, search: NSPredicate(format: "contactName == %@", name)) 

recs = recordsInTable(Contact.self) 
print ("Contacts table has \(recs) records") 

saveDatabase()