问题描述
我正在使用 MVVM 架构在 swift 上开发一个简单的 tableView
项目。但是当我点击那个 tableView 上的按钮时,我收到了这样的错误:
线程1:同时访问0x7faf490331c8,但修改需要独占访问
我已经定义了一个这样的模型
struct Person{
var name: String
var username : String
var currentFollowing : Bool
let image : UIImage?
}
protocol PersonFollowingTableViewCellDelegate : AnyObject {
func PersonFollowingTableViewCell( _ cell: PersonFollowingTableViewCell,didTapWith : Person)
}
我正在尝试使用它来配置按钮
@objc private func didTapButton(){
let defaultPerson = Person(name: "default",username: "default",currentFollowing: true,image: nil)
person?.currentFollowing = !(person?.currentFollowing ?? true)
delegate?.PersonFollowingTableViewCell(self,didTapWith: person ?? defaultPerson )
prepareForReuse()
configure(with: person ?? defaultPerson)
}
extension ViewController : PersonFollowingTableViewCellDelegate{
func PersonFollowingTableViewCell(_ cell: PersonFollowingTableViewCell,didTapWith array: Person) {
return
}
解决方法
修改你的@objc 私有函数 didTapButton()。 而不是
person?.currentFollowing = !(person?.currentFollowing ?? true)
制作
let currentFollowing = !(person?.currentFollowing ?? true)
person?.currentFollowing = currentFollowing