How to convert an Int into NSData in Swift?

前端 未结 7 1908
陌清茗
陌清茗 2021-02-03 22:37

In Objective-C I use the following code to

  1. Convert an Int variable into NSData, a packet of bytes.

    int myScore = 0;
    NS         
    
    
            
7条回答
  •  误落风尘
    2021-02-03 22:39

    With Swift 3.x to 5.0:

    var myInt = 77
    var myIntData = Data(bytes: &myInt, 
                         count: MemoryLayout.size(ofValue: myInt))
    

提交回复
热议问题