Inter-operability of Swift arrays with C?

前端 未结 4 1930
终归单人心
终归单人心 2021-02-02 09:46

How can one pass or copy the data in a C array, such as

float foo[1024];

, between C and Swift functions that use fixed size arrays, such as d

4条回答
  •  太阳男子
    2021-02-02 10:31

    let's see what Apple do:

    public struct float4 {
    
        public var x: Float
    
        public var y: Float
    
        public var z: Float
    
        public var w: Float
    
        /// Initialize to the zero vector.
        public init()
    
        /// Initialize a vector with the specified elements.
        public init(_ x: Float, _ y: Float, _ z: Float, _ w: Float)
    
        /// Initialize a vector with the specified elements.
        public init(x: Float, y: Float, z: Float, w: Float)
    
        /// Initialize to a vector with all elements equal to `scalar`.
        public init(_ scalar: Float)
    
        /// Initialize to a vector with elements taken from `array`.
        ///
        /// - Precondition: `array` must have exactly four elements.
        public init(_ array: [Float])
    
        /// Access individual elements of the vector via subscript.
        public subscript(index: Int) -> Float
    }
    

提交回复
热议问题