【发布时间】:2015-11-26 09:21:15
【问题描述】:
我已将 .csv 文件中的数据预加载到 coredata 中。我正在通过以下方式获取数据
var places:[Places] = []
在viewDidLoad:
if let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext {
let fetchRequest = NSFetchRequest(entityName: "Places")
do{
places = try managedObjectContext.executeFetchRequest(fetchRequest) as! [Places]
}
catch let error as NSError{
print("Failed to retrieve record: \(error.localizedDescription)")
}
}
数据中有一个String类型的属性isFavorite,其初始值为false。我正在更改按钮单击时 isFavorite 的值。我想保存用户所做的更改。 我怎样才能使这种变化持久化?
这是我的按钮操作
@IBAction func addToFavourites(sender: AnyObject) {
cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: sender.tag, inSection: 0)) as! CustomTableViewCell
if cell.isFavouriteLabel.text! == "false" {
cell.isFavouriteLabel.text = "true"
}else if cell.isFavouriteLabel.text == "true"{
cell.isFavouriteLabel.text = "false"
}
}
基本上我想设置places.isFavourite = cell.isFavoriteLabel.text的值并保存到核心数据
编辑:如果我在我的按钮操作方法中尝试这个
if let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext {
let place : Places = Places()
place.isFavourite = cell.isFavouriteLabel.text
do{
try managedObjectContext.save()
} catch let error as NSError{
print(error)
}
}
我收到一个错误:无法在 NSManagedObject 类上调用指定的初始化程序
如果我只是在按钮操作方法中添加此代码
places.isFavourite = cell.isFavouriteLabel.text
我收到此错误:[Places] 没有名为 isFavourite 的成员
【问题讨论】:
标签: ios iphone xcode swift core-data