简体   繁体   English

Struct中的Swift Init可选数组

[英]Swift Init Optional Array in Struct

I can do an element with optinal like below but I dont know how create optional Array in Struct. 我可以像下面这样用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
      }
}

I get an exception below code that nil for UIImage Array. 我在UIImage Array无效的代码下面得到一个异常。 How can I do when the Images input comes as 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
      }
}

You have to declare the variable as Optional. 您必须将变量声明为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
     }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM