2

I am converting by Obj-C code to Swift, but from last 2 days I am stuck on third line in below Obj-C code.

char encodedChar = (int)c + (int)kc;

NSLog(@"Encoded Char : %c", encodedChar);     //prints like, Î, ¾, Ü etc.

const void *pt = &encodedChar;

[encodedData appendData:[NSData dataWithBytes:pt length:1]];

And this is my Swift Code,

let encodedChar = "\(character)".toInt()! + numbers[index]

let characterString = String(stringInterpolationSegment: UnicodeScalar(encodedChar))

println("Encoded Char String : \(characterString)")    //prints same, Î, ¾, Ü etc.

But what next, what would be swift equivalent of : const void *pt = &encodedChar;

I do not want to convert my string to NSData. Because it behaves differently.

Any suggestion will be helpful, Thanks :)

itsji10dra
  • 4,603
  • 3
  • 39
  • 59
  • Possible duplicate of [Creating NSData from NSString in Swift](http://stackoverflow.com/questions/24039868/creating-nsdata-from-nsstring-in-swift). Swift won't let you access character pointers "just like that" from a `String`. – zneak May 07 '15 at 06:16
  • I think you don't need to do that you can also use this objective-c class in swift by Bridging look at this link http://stackoverflow.com/questions/24002369/how-to-call-objective-c-code-from-swift – Harish May 07 '15 at 06:18
  • @harish Thnxx for suggesstion, but i need to use pure swifty code. – itsji10dra May 07 '15 at 06:21
  • @zneak Accessing pointers and converting into Data are two different things, how can it be duplicate. – itsji10dra May 07 '15 at 06:24
  • @RoNiT, I thought you wanted to append it as data. That's what your Objective-C code does. – zneak May 07 '15 at 06:25
  • @Zneak Thnxx your suggestion worked, i got my answer :) – itsji10dra May 07 '15 at 06:53

1 Answers1

1

Got answer,

var encodedChar = "\(character)".toInt()! + keyNumbers[index]

let data = NSData(bytes: &encodedChar, length: 1)
encodedData.appendData(data)
itsji10dra
  • 4,603
  • 3
  • 39
  • 59