歡迎加入QQ討論群258996829

CoreValue

發(fā)布時(shí)間:2016-01-05 15:47  回復(fù):0  查看:4719  感興趣:11  贊:1   最后回復(fù):2016-01-05 15:47  

CoreValue是Core Data的輕量級(jí)封裝框架。它也包含了一些簡(jiǎn)單的抽象,便于查詢,更新,保存和刪除。

  • 繼承類

struct Shop: CVManagedPersistentStruct {

        // The name of the CoreData entity
        static let EntityName = "Shop"

        // The ObjectID of the CoreData object we saved to or loaded from
        var objectID: NSManagedObjectID?

        // Our properties
        let name: String
        var age: Int32
        var owner: Owner?

        // Create a Value Type from a NSManagedObject
        // If this looks too complex, see below for an explanation and alternatives
        static func fromObject(o: NSManagedObject) -> Unboxed<Shop> {
            return curry(self.init)
                <^> o <|? "objectID"
                <*> o <| "name"
                <*> o <| "age"
                <*> o <|? "owner"
        }
    }

  • 創(chuàng)建、保存、刪除

    // Get all shops (`[Shop]` is required for the type checker to get your intent!)
    let shops: [Shop] = Shop.query(self.context, predicate: nil)

    // Create a shop
    let aShop = Shop(name: "Household Wares", age: 30, owner: nil)

    // Store it as a managed object
    aShop.save(self.context)

    // Change the age
    aShop.age = 40

    // Update the managed object in the store
    aShop.save(self.context)

    // Delete the object
    aShop.delete(self.context)

    // Convert a managed object into a shop (see below)
    let nsShop: Shop? = Shop.fromObject(aNSManagedObject).value

    // Convert a shop into an nsmanagedobject
    let shopObj = nsShop.mutatingToObject(self.context)

  • 查詢

// With Sort Descriptors
public static func query(context: NSManagedObjectContext, predicate: NSPredicate?, sortDescriptors: Array<NSSortDescriptor>) -> Array

// Without sort descriptors
public static func query(context: NSManagedObjectContext, predicate: NSPredicate?) -> Array

相關(guān)開(kāi)源代碼

您還未登錄,請(qǐng)先登錄

熱門帖子

最新帖子

?