【问题标题】:Array initializer for custom types自定义类型的数组初始化器
【发布时间】:2016-01-21 07:06:07
【问题描述】:

如何使用构造函数将自定义类型转换为数组?

 extension Array where Generator.Element == Int{ // same-type requirement makes generic parameter 'Element' non-generic
// `where Element: SignedIntegerType` would work but that is a protocol 
        init(_ value:Custom){
            self = [value.item1, value.item2, value.item3, value.item4 ] // cannot assign value of type '[Int]' to type 'Array<_>'
        }
    }

    struct Custom{
        // let array = [item.........] I could also have an array here but that is not the point of the question. 
        private let item1:Int
        private let item2:Int
        private let item3:Int
        private let item4:Int

        init(_ value1:Int, _ value2:Int, _ value3:Int, _ value4:Int ){
            self.item1 = value1
            self.item2 = value2
            self.item3 = value3
            self.item4 = value4

        }
    }

    let custom = Array(Custom(2, 3, 4, 5))// I want to be be able to convert to an array/set. 

编辑:我认为这可能是 swift 2.1 的限制

【问题讨论】:

    标签: arrays swift swift2


    【解决方案1】:

    带有自定义类型的 Swift 3 示例:

    假设您将 MyCustomType 作为自定义类:

    class MyCustomType
    {
        var name    = ""
        var code    = ""
    
        convenience init(name: String, code: String)
        {
            self.init()
    
            self.name = name
            self.code = code
        }
    }
    

    你可以这样扩展数组:

    extension Collection where Iterator.Element == MyCustomType
    {
        func getNameFrom(code: String) -> String?
        {
            for custom in self {
                if custom.code == code {
                    return custom.name
                }
            }
            return nil
        }
    }
    

    用法:

    let myCustomArray = [MyCustomType]()
    
    myCustomArray.getNameFrom(code: "code")
    

    希望对您有所帮助! :)

    【讨论】:

    • 是的。 Swift 3 克服了这个限制。谢谢!
    【解决方案2】:

    我不确定你为什么需要它作为 Array 类型的扩展。

    您的结构(或类)可以简单地具有一个将其内容作为数组返回的方法。语法会略有不同,但据我从您的示例中可以看出,结果将是相同的。

    struct Custom<T>
    {
        private let item1:T
        private let item2:T
        private let item3:T
        private let item4:T
    
        init(_ value1:T, _ value2:T, _ value3:T, _ value4:T )
        {
            self.item1 = value1
            self.item2 = value2
            self.item3 = value3
            self.item4 = value4
    
        }
    
        var array:[T] { return [item1, item2, item3, item4] }
    }
    
    let custom = Custom(2, 3, 4, 5).array
    

    【讨论】:

      【解决方案3】:

      这个怎么样?:

      struct Custom{
          private let item1:Int
          private let item2:Int
          private let item3:Int
          private let item4:Int
      
          init(_ value1:Int, _ value2:Int, _ value3:Int, _ value4:Int ){
              self.item1 = value1
              self.item2 = value2
              self.item3 = value3
              self.item4 = value4
          }
      
          var array : [Int] {
              get {
                  return [self.item1, self.item2, self.item3, self.item4]
              }
          }
      }
      
      let customArray = Custom(2, 3, 4, 5).array // [2, 3, 4, 5]
      

      或简化为:

      struct Custom {
          let array: [Int]
          init(_ value: Int...) {
              self.array = value
          }
      }
      
      let customArray = Custom(2, 3, 4, 5).array // [2, 3, 4, 5]
      

      【讨论】:

        【解决方案4】:

        试试看这个“例子”

        struct Custom {
            let array: [Int]
            init(_ value: Int...) {
                self.array = value.map({$0+1})
            }
        }
        
        extension _ArrayType where Generator.Element == Int {
            init(custom: Custom) {
                self.init()
                self.appendContentsOf(custom.array)
            }
        }
        let custom = Custom(1,2,3)
        let carr = Array(custom: custom)
        
        print(carr, carr.dynamicType) // [2, 3, 4] Array<Int>
        

        在你使用这样的东西之前,请自己回答这个问题 为什么不简单使用

        let carr = custom.array
        

        ???

        【讨论】:

        • 我更改了代码以演示一个更具体的情况,即使用数组作为字段。
        • 您使用的是私有的_ArrayType 类型,我认为这对于稳定的代码是不可行的。
        • @masters3d 请看,我不建议您使用 _ArrayType!我建议您直接使用 .array 属性。您的“更具体的情况”不是那么具体,您可以轻松定义数组属性。在我的示例中,我试图向您展示错误在哪里:相同类型的要求使泛型参数“元素”非泛型来自。
        • @masters3d 顺便说一句,_ArrayType 是公共协议,但我同意,它是为“内部”使用而设计的。 Swift 非常新,你可以期待未来版本之间会有很多变化。是否以_(下划线)为前缀:-)。一般来说,你想做什么,不是最好的主意(这是我自己的观点)
        猜你喜欢
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多