英文:
How to assign variables in same class that use each other when getting data from previous view
问题
我之前加载的视图控制器从我的数据库中获取不同的视频并获取它们的下载链接,然后我使用prepare(segue: ...)
将每个链接传递给这个下一个视图控制器,每个链接都使用它自己的字符串。在这个类中,我使用编程方式创建了一个包含自定义数据的滚动视图,但是我收到了错误消息“无法在属性初始化器内使用实例成员'itemOne';属性初始化器在'self'可用之前运行”。我理解这个错误的含义,我尝试过使用延迟变量,但考虑到前一个视图控制器需要该变量来发送URL数据,我认为它不会起作用。所以当你需要向那个变量发送数据时,你会怎么做呢?下面是我的代码:
class HomeViewController: UIViewController {
// 用于在前一个视图控制器中发送数据的变量
var itemOne: String?
var itemTwo: String?
var itemThree: String?
var itemFour: String?
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(collectionView)
collectionView.backgroundColor = .white
collectionView.topAnchor.constraint(equalTo: view.topAnchor, constant: 150).isActive = true
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16).isActive = true
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16).isActive = true
collectionView.heightAnchor.constraint(equalTo: collectionView.widthAnchor, multiplier: 0.5).isActive = true
collectionView.delegate = self
collectionView.dataSource = self
}
fileprivate let data: [CustomData] = [
CustomData(title: "Test", image: #imageLiteral(resourceName: "ss-1"), url: itemOne!), // 错误
CustomData(title: "Test2", image: #imageLiteral(resourceName: "done-button"), url: itemTwo!), // 错误
CustomData(title: "Test3", image: #imageLiteral(resourceName: "notificationIcon"), url: itemThree!) // 错误
]
// ...
}
上面的代码中有一些问题,因为在属性初始化器中,无法使用实例成员(例如itemOne
、itemTwo
等)来初始化属性。为了解决这个问题,你可以将数据初始化移到viewDidLoad
方法中,这样就可以使用这些实例成员了。请注意,你可能需要处理itemOne
等变量为nil
的情况,以避免潜在的崩溃。
英文:
I have a view Controller before this one is loaded that gets different videos from my database and gets their download url, then I pass each url to this next viewcontroller in it's own String using prepare(segue:...)
, In this class I have programatically made a scrollview with my custom data, however I get the error Cannot use instance member 'itemOne' within property initializer; property initializers run before 'self' is available
. I understand what this error means and I've tried lazy variables but I don't think it would work considering the previous view controller needs that variable to send the url data to, if that makes sense. So how would you go about this when you need to send data to that variable, here's my code:
class HomeViewController: UIViewController {
// Variables use with the previous view controller to send data between them
var itemOne: String?
var itemTwo: String?
var itemThree: String?
var itemFour: String?
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(collectionView)
collectionView.backgroundColor = .white
collectionView.topAnchor.constraint(equalTo: view.topAnchor, constant: 150).isActive = true
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16).isActive = true
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16).isActive = true
collectionView.heightAnchor.constraint(equalTo: collectionView.widthAnchor, multiplier: 0.5).isActive = true
collectionView.delegate = self
collectionView.dataSource = self
}
fileprivate let data = [
CustomData(title: "Test", image: #imageLiteral(resourceName: "ss-1"), url: itemOne!), // ERROR
CustomData(title: "Test2", image: #imageLiteral(resourceName: "done-button"), url: itemTwo!), // ERROR
CustomData(title: "Test2", image: #imageLiteral(resourceName: "notificationIcon"), url: itemThree!) // ERROR
]
/....
答案1
得分: 0
你可以将数据作为计算属性:
var data: [CustomData] {
return [
CustomData(title: "Test", image: #imageLiteral(resourceName: "ss-1"), url: itemOne!),
CustomData(title: "Test2", image: #imageLiteral(resourceName: "done-button"), url: itemTwo!),
CustomData(title: "Test2", image: #imageLiteral(resourceName: "notificationIcon"), url: itemThree!)
]
}
英文:
You can make data a computed property
var data: [CustomData] {
return [
CustomData(title: "Test", image: #imageLiteral(resourceName: "ss-1"), url: itemOne!),
CustomData(title: "Test2", image: #imageLiteral(resourceName: "done-button"), url: itemTwo!),
CustomData(title: "Test2", image: #imageLiteral(resourceName: "notificationIcon"), url: itemThree!)
]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论