Creating NSData from NSString in Swift

前端 未结 9 2347
情深已故
情深已故 2020-11-27 11:11

I\'m trying to ultimately have an NSMutableURLRequest with a valid HTTPBody, but I can\'t seem to get my string data (coming from a UITextFie

相关标签:
9条回答
  • 2020-11-27 12:08

    To create not optional data I recommend using it:

    let key = "1234567"
    let keyData = Data(key.utf8)
    
    0 讨论(0)
  • 2020-11-27 12:09

    Swift 4.2

    let data = yourString.data(using: .utf8, allowLossyConversion: true)
    
    0 讨论(0)
  • 2020-11-27 12:14

    Here very simple method

    let data = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
    
    0 讨论(0)
提交回复
热议问题