Realm数据库
github: https://github.com/realm/realm-swift
文档介绍:https://wenku.baidu.com/view/0d407c5c8d9951e79b89680203d8ce2f0066650c.html
// Define your models like regular Swift classes class Dog: Object { @Persisted var name: String @Persisted var age: Int } class Person: Object { @Persisted(primaryKey: true) var _id: String @Persisted var name: String @Persisted var age: Int // Create relationships by pointing an Object field to another Class @Persisted var dogs: List<Dog> } // Use them like regular Swift objects let dog = Dog() dog.name = "Rex" dog.age = 1 print("name of dog: \(dog.name)") // Get the default Realm let realm = try! Realm() // Persist your data easily with a write transaction try! realm.write { realm.add(dog) }
// Open the default realm. let realm = try! Realm() var token: NotificationToken? let dog = Dog() dog.name = "Max" // Create a dog in the realm. try! realm.write { realm.add(dog) } // Set up the listener & observe object notifications. token = dog.observe { change in switch change { case .change(let properties): for property in properties { print("Property '\(property.name)' changed to '\(property.newValue!)'"); } case .error(let error): print("An error occurred: (error)") case .deleted: print("The object was deleted.") } } // Update the dog's name to see the effect. try! realm.write { dog.name = "Wolfie" }
// Generate a random encryption key var key = Data(count: 64) _ = key.withUnsafeMutableBytes { bytes in SecRandomCopyBytes(kSecRandomDefault, 64, bytes) } // Add the encryption key to the config and open the realm let config = Realm.Configuration(encryptionKey: key) let realm = try Realm(configuration: config) // Use the Realm as normal let dogs = realm.objects(Dog.self).filter("name contains 'Fido'")
签 名:被别人嫉妒,说明你卓越;你嫉妒别人,说明你无能。
座右铭:抓紧一切时间睡觉。