Search code examples
objective-cswiftnsdata

Converting issue Objective-C to Swift


NSData *myRequestData = [NSData dataWithBytes: [encryptedStr UTF8String] length: [encryptedStr length]];

I want to convert the above string to Swift.

var myRequestData = NSData(bytes: encryptedStr.UTF8String(), length: encryptedStr.length())!

I have tried something above but it is not working in Xcode 7.3.1.

I have also tried to convert using https://objectivec2swift.com/#/home/main but failed.


Solution

  • try this

    var myRequestData = NSData(bytes: (encryptedStr as NSString).UTF8String, length: encryptedStr.characters.count)