大佬教程收集整理的这篇文章主要介绍了ios – 如何使用Swift使用Core Data更新/保存和保留非标准(可转换)属性?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
import Foundation import CoreData class DesTination: NsmanagedObject { @Nsmanaged var name: String @Nsmanaged var LOCATIOn: LOCATIOn }
…具有简单的名称属性(String类型)和类型为LOCATIOn的属性:
import Foundation class LOCATIOn: NSObject,NSCoding { var address: String var latitude: Double var longitude: Double required init?(coder aDecoder: NSCoder) { address = aDecoder.decodeObjectForKey("Address") as? String ?? "" latitude = aDecoder.decodeObjectForKey("Latitude") as? Double ?? 0.0 longitude = aDecoder.decodeObjectForKey("Longitude") as? Double ?? 0.0 super.init() } init(address: String,latitude: Double,longitude: DoublE) { self.address = address self.latitude = latitude self.longitude = longitude super.init() } func encodeWithCoder(aCoder: NSCoder) { aCoder.encodeObject(address,forKey: "Address") aCoder.encodeObject(latitude,forKey: "Latitude") aCoder.encodeObject(longitude,forKey: "Longitude") } }
位置在Core Data中配置为“可转换”,因为它具有其他基本类型都无法处理的结构.
使用Apple的样板核心数据代码,这里是一个视图控制器,它只执行以下操作:
>获取必要的appDelegate / ManagedApplicationContext引用
>如果存在目的地,则获取目的地,否则创建一个目的地
>打印目的地的名称和LOCATIOn.address
>更新目标的名称和LOCATIOn.address
>保存对ManagedObjectContext的更改
当应用程序运行并重新运行时,只会对名称进行更改.对LOCATIOn.address所做的更改不会保留.
import UIKit import CoreData class ViewController: UIViewController { var appDelegate: AppDelegate! var context: NsmanagedObjectContext! override func viewDidLoad() { super.viewDidLoad() updateDesTination() } func updateDesTination() { var desTination: DesTination appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate context = appDelegate.managedObjectContext if let dest = fetchOneDesTination() { desTination = dest } else { desTination = create()! } print("desTination named: \(desTination.Name),at: \(desTination.LOCATIOn.address)") desTination.name = "New name of place that will update and persist" desTination.LOCATIOn.address = "123 main st (change that will never persist)" appDelegate.saveContext() } func create() -> DesTination? { guard let newDesTination = NSEntityDescription.insertNewObjectForEntityForName("DesTination",inManagedObjectContext: context) as? DesTination else { return nil } newDesTination.name = "Original name of place that can be updated" newDesTination.LOCATIOn = LOCATIOn(address: "100 main st",latitude: 34.051145,longitude: -118.243595) return newDesTination } func fetchOneDesTination() -> DesTination? { let request = NSFetchrequest() request.entity = NSEntityDescription.entityForName("DesTination",inManagedObjectContext: context) do { let fetchResults = try context.executeFetchrequest(request) if fetchResults.count > 0 { if let dest = fetchResults[0] as? DesTination { return dest } } } catch {} return nil } }
如何更新目标的位置属性?
以上是大佬教程为你收集整理的ios – 如何使用Swift使用Core Data更新/保存和保留非标准(可转换)属性?全部内容,希望文章能够帮你解决ios – 如何使用Swift使用Core Data更新/保存和保留非标准(可转换)属性?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。