【发布时间】:2014-11-20 10:29:56
【问题描述】:
我有这个模型:
enum PhotoState: String {
case New = "New"
case Downloaded = "Downloaded"
case Failed = "Failed"
}
class Photo: RLMObject {
dynamic var id = 0
dynamic var name = ""
dynamic var imageURLString = ""
var state = PhotoState.New
override class func primaryKey() -> String! {
return "id"
}
override class func ignoredProperties() -> [AnyObject]! {
return ["state"]
}
}
我在表格视图中展示这个:
override func viewDidLoad() {
super.viewDidLoad()
photos = Photo.allObjects()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
if let count = photos?.count {
return Int(count)
}
return 0
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let photo = photos?.objectAtIndex(UInt(indexPath.row)) as? Photo {
downloadPhoto(photo, indexPath: indexPath)
}
}
func downloadPhoto(photo: Photo, indexPath: NSIndexPath) {
// Fake Download
switch photo.imageURLString.lastPathComponent {
case "7.png":
photo.state = .Failed
default:
photo.state = .Downloaded
}
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}
如果我点击一行,该行应该会更新并具有新的状态,但照片记录的 state 属性又回到了 .New。
RLMResult 的 objectForIndex 是否初始化了一个新的模型实例?
【问题讨论】: