qin*_*hen 2 cocoa cocoa-touch ios swift
我正在Swift中编写一个iOS应用程序,并且在结构和块方面存在一些问题.当用户编辑他的教育信息并单击保存按钮时,我向服务器发送请求并更新成功处理程序中的本地数据.在这里,我editItem()在类中编写了一个函数,EditController并editEducation()在类UserModel结构中编写了一个函数:
class EditController: UITableViewController {
//...
func editItem<T>(item: T, completion: () -> Void) {
switch selectedType {
case .Education:
let educationItem = item as! EducationModel
// self.user is a object of type "UserModel"
user!.editEducation(index: selectedRow, educationItem: educationItem) {
// breakpoint 2 here
completion()
self.tableView.reloadData()
}
default:
return
}
}
//...
}
struct UserModel {
//...
var education = [EducationModel]()
//EducationModel is also a structure
//...
mutating func editEducation(#index: Int, educationItem: EducationModel, completion: () -> Void) {
let manager = HTTPClient.sharedManager
manager.POST("/user/education/update",
parameters: [
// params
],
success: { (task, responseObject) in
if responseObject["status"] as! Int == 0 {
self.education[index] = educationItem
// breakpoint 1 here
completion()
} else {
println(responseObject)
}
},
failure: { (task, error) in
println(error)
}
)
}
//...
}
Run Code Online (Sandbox Code Playgroud)
问题是,尽管self.education在程序到达断点1时已成功更新(如上所述),self.user.education仍然包含断点2处的旧数据.如果我移动
self.education[index] = educationItem
Run Code Online (Sandbox Code Playgroud)
在POST请求之外,数据在断点2处按预期更新.我尝试通过在断点2添加调度来延迟,但它仍然不会更新.我还打印了相关变量的内存地址,但它没有提供有用的信息.我现在加入了处理这个controller参数editEducation,通过self在EditController与力更新user控制器的性能.我认为这不是一个合适的解决方案.那么有更好的方法吗?
更新1
我尝试将UserModel变成一个类,它解决了这个问题,但是我不想那样做,因为:首先,如果我把它变成一个类,那么应用程序其他部分的某些行为可能会变得奇怪将模型分成几类以确保一致性.
你在这里看到的是值类型(struct)和引用类型(class)之间的区别.您应该阅读Apple网站上关于这些差异的博客文章:https://developer.apple.com/swift/blog/?id = 10
基本上,一旦你将一个struct传递给一个块,它就会被复制而不是被引用,所以你正在开发一个全新的副本.使用类,您可以获得原始副本的引用.
| 归档时间: |
|
| 查看次数: |
594 次 |
| 最近记录: |