String 를 NSData 로, NSData 를 String 로

swift에서 String를 NSData로, NSData를 String로 바꾸는 코드입니다.

var swift_string = "lorem ipsum dolor sit amet"
// from String to NSData
let data = swift_string.dataUsingEncoding(NSUTF8StringEncoding)
print(data)
// from NSData to String
var out: String = String(data:data!, encoding:NSUTF8StringEncoding)!
print(out) // print "lorem ipsum dolor sit amet"

This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.