[英]Save contacts into tableview using core data
I have an app that uses the Contacts framework for iOS 9+. 我有一个使用iOS 9+的通讯录框架的应用程序。 I have made a tableview that holds some selected contacts by the users.
我制作了一个表格视图,其中包含用户选择的一些联系人。 I want to be able to persist this list of contacts.
我希望能够保留此联系人列表。 what is the best way to do this?
做这个的最好方式是什么? I was thinking of using core data.
我当时在考虑使用核心数据。
My problem is that the contacts are "var Contacts = CNContact" hence are not of type NSManagedObject. 我的问题是联系人是“ var Contacts = CNContact”,因此不是NSManagedObject类型。 I was under the impression that a to save data using core data the variables had to be of type NSManagedObject and my variables are of type CNContact.
我的印象是,要使用核心数据保存数据,变量必须为NSManagedObject类型,而我的变量为CNContact类型。
Here's an example of using plists to hold your data. 这是一个使用复制人保存数据的示例。 Lets say you have an object such as a "User".
假设您有一个对象,例如“用户”。 This user has a username and password for example.
例如,此用户具有用户名和密码。
Then in your user class you should have something like this: 然后,在用户类中,您应该具有以下内容:
class User: NSObject, NSCoding {
var username: String
var password: String
required init?(coder aDecoder: NSCoder) {
username = aDecoder.decodeObject(forKey: "Username") as! String
password = aDecoder.decodeObject(forKey: "Password") as! String
}
init(username: String, password: String, subjects: [Subject], colorPreferences: [Subject: UIColor], assessments: [Assessment]) {
self.username = username
self.password = password
}
func encode(with aCoder: NSCoder) {
aCoder.encode(username, forKey: "Username")
aCoder.encode(password, forKey: "Password")
}
}
Then what you need to do in a viewController, where you are using the user object is the following: 然后,您需要在使用用户对象的viewController中执行以下操作:
var loggedInUser: User?
class MyViewController: UIViewController {
func documentsDirectory() -> String {
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
return paths[0]
}
func dataFilePath() -> String {
return (documentsDirectory() as NSString).appendingPathComponent("AppInfo.plist") //name of your plist
}
func saveData() {
let data = NSMutableData()
let archiver = NSKeyedArchiver(forWritingWith: data)
archiver.encodeObject(loggedInUser, forKey: "Logged In User") //whatever key you want
archiver.finishEncoding()
data.write(toFile: dataFilePath(), atomically: true)
}
func loadData() {
let path = dataFilePath()
if FileManager.default.fileExists(atPath: path){
if let data = NSData(contentsOfFile: path){
let unarchiver = NSKeyedUnarchiver(forReadingWith: data as Data)
loggedInUser = unarchiver.decodeObjectForKey("Logged In User") as? UserProfile
unarchiver.finishDecoding()
}
}
}
}
So this is a simplified version, but you should be able to adapt it to hold your list of CNContacts. 因此,这是一个简化的版本,但是您应该能够对其进行修改,以保留您的CNContacts列表。 Let me know if there are any issues.
让我知道是否有任何问题。 I use this code in my own apps so it should work just fine.
我在自己的应用中使用此代码,因此它应该可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.