NSData from UInt8

前端 未结 3 1261
心在旅途
心在旅途 2020-12-18 23:40

I have recently found a source code in swift and I am trying to get it to objective-C. The one thing I was unable to understand is this:

var theData:UInt8!

         


        
3条回答
  •  清酒与你
    2020-12-19 00:17

    In Swift,

    Data has a native init method.

    // Foundation -> Data  
    
    /// Creates a new instance of a collection containing the elements of a
    /// sequence.
    ///
    /// - Parameter elements: The sequence of elements for the new collection.
    ///   `elements` must be finite.
    @inlinable public init(_ elements: S) where S : Sequence, S.Element == UInt8
    
    @available(swift 4.2)
    @available(swift, deprecated: 5, message: "use `init(_:)` instead")
    public init(bytes elements: S) where S : Sequence, S.Element == UInt8
    

    So, the following will work.

    let values: [UInt8] = [1, 2, 3, 4]
    let data = Data(values)
    

提交回复
热议问题