gpt4 book ai didi

ios - 设置对象或调用函数 EXC_BAD_ACCESS 时出错(代码=257)

转载 作者:行者123 更新时间:2023-11-30 11:24:55 26 4
gpt4 key购买 nike

我创建了一些自定义 UIView,并在我的项目中的另一个 UIView 中实现。我在调用某些函数或为某些对象设置值时发生崩溃,但在设置属性时没有发生崩溃。请参阅下面的代码以正确理解代码和问题。

class SearchProfileDetailView: UIView {

@IBOutlet weak var userInfoTable: UITableView!

var userObject: UserObject?

override init(frame:CGRect) {
super.init(frame: frame)
loadViewFromNib()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
loadViewFromNib()
}

func loadViewFromNib() {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "SearchProfileDetailView", bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
view.frame = bounds
self.addSubview(view);

}

func setColor() {
self.backgroundColor = UIColor.red
}
}

我已经在另一个 UIView 中使用了这个 SearchProfileDetailView 以及界面构建器和分配的 socket ,请参阅下面的代码。

class SearchResultView: UIView {

@IBOutlet weak var profileDetailView: SearchProfileDetailView!

var currentIndex: Int?

override init(frame:CGRect) {
super.init(frame: frame)
loadViewFromNib()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
loadViewFromNib()
}

func loadViewFromNib() {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "SearchResultView", bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
view.frame = bounds
self.addSubview(view);
}
func setUserProfile(userObject: UserObject) {
profileDetailView.backgroundColor = UIColor.red // Not Crashed
profileDetailView.userObject = userObject //Crashed with error mentioned in title of question
profileDetailView.setColor() ////Crashed with error mentioned in title of question
}
}

Crashed while calling profileDetailView.setColor() and profileDetailView.userObject = userObject But not crashed on profileDetailView.backgroundColor = UIColor.red

现在我已经通过界面生成器将这个 SearchResultView 使用到 UIViewController 中。

class ExploreViewController: UIViewController {

@IBOutlet weak var searchResultView: SearchResultView!

func getResult() {
SearchAPIHandler().search(params: parameters) {[weak self] (responseData) in
DispatchQueue.main.async {
guard let strongSelf = self else {
return
}
if let users = responseData.users, !users.isEmpty {
strongSelf.searchResultView.setUserProfile(userObject: users[0])
}
}

}
}

最佳答案

我已经尝试了很多,现在我知道问题出在我的界面生成器上。

没有模块。

我检查了“从目标继承模块”,一切正常。

enter image description here

关于ios - 设置对象或调用函数 EXC_BAD_ACCESS 时出错(代码=257),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50850720/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com