我可以像下面那样使用optinal来做一个元素,但是我不知道如何在Struct中创建可选数组。
struct HomeElement {
var title:String!
var description:String?
var body:String!
init(title:String!,description:String,body:String!) {
self.title = title
self.description = description
self.body = body
}
}
我在UIImage Array的代码为nil的代码下出现异常。当“图片”输入为零时,我该怎么办。
struct HomeElement {
var title:String!
var description:String?
var body:String!
var images = [UIImage]()
init(title:String!,description:String,body:String!,images:[UIImage]) {
self.title = title
self.description = description
self.body = body
self.images = images
}
}
答案 0 :(得分:1)
您必须将变量声明为Optional。
struct HomeElement {
var title:String!
var description:String?
var body:String!
var images: [UIImage]?
init(title:String!,description:String,body:String!,images:[UIImage]?) {
self.title = title
self.description = description
self.body = body
self.images = images
}
}