0
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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
ZetrixWeb
  • 47
  • 1
  • 8

1 Answers1

0

try this

var myRequestData = NSData(bytes: (encryptedStr as NSString).UTF8String, length: encryptedStr.characters.count)
Mahesh Verma
  • 174
  • 14
  • Thanks @Mahesh Verma – ZetrixWeb Aug 25 '16 at 05:05
  • NSString *encVal = [ccTool encryptRSA:myRequestString key:rsaKey]; encVal = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)encVal, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8 )); – ZetrixWeb Aug 25 '16 at 05:07
  • can you please also convert this ? – ZetrixWeb Aug 25 '16 at 05:07
  • @ZetrixWeb Looks like you are using some component which is written in objective c (for encryption). So firstly add a bridge to those components. Then try integrating these components. – Mahesh Verma Aug 25 '16 at 05:12
  • i have already added i am trying to integrate CCAvenue in swift – ZetrixWeb Aug 25 '16 at 05:15
  • CFURLCreateStringByAddingPercentEscapes is deprecated, So better use it like let charset = NSCharacterSet(charactersInString: "/%&=?$#+-~@<>|\\*,.()[]{}^!").invertedSet let escaped = orig.stringByAddingPercentEncodingWithAllowedCharacters(charset) – Mahesh Verma Aug 25 '16 at 05:25
  • This all i am doing to integrate CCAvenue Payment Gateway in swift , have you ever implemented ? – ZetrixWeb Aug 25 '16 at 05:26
  • You can have look at this http://stackoverflow.com/questions/35215338/string-encoding-swift – Mahesh Verma Aug 25 '16 at 05:26
  • `encryptedStr.characters.count` is *not* the correct value to pass as the length parameter to `NSData`. Try it with `encryptedStr = "€"`. The correct result should be ``, not ``. – Martin R Aug 25 '16 at 05:42