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 UITextField
) into a usable NSData
object.
I've seen this method for going the other way:
NSString(data data: NSData!, encoding encoding: UInt)
But I can't seem to find any documentation for my use case. I'm open to putting the string into some other type if necessary, but none of the initialization options for NSData
using Swift seem to be what I'm looking for.
Swift 4
In swift 5
Swift 4 & 3
Creating
Data
object fromString
object has been changed in Swift 3. Correct version now is:Here very simple method
To create not optional data I recommend using it: