爲Base64我想一個字符串轉換爲一個base64。我在幾個地方找到了同樣的答案,但它不能迅速工作。我正在使用Xcode 6.2。我相信答案可能是以前的Xcode而不是Xcode 6.2。如何編碼字符串中迅速
可能有人請引導我做這在Xcode 6.2?
我找到了答案是這樣的。但它並沒有在Xcode 6.2工作
var str = "iOS Developer Tips encoded in Base64"
println("Original: \(str)")
// UTF 8 str from original
// NSData! type returned (optional)
let utf8str = str.dataUsingEncoding(NSUTF8StringEncoding)
// Base64 encode UTF 8 string
// fromRaw(0) is equivalent to objc 'base64EncodedStringWithOptions:0'
// Notice the unwrapping given the NSData! optional
// NSString! returned (optional)
let base64Encoded = utf8str.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.fromRaw(0)!)
println("Encoded: \(base64Encoded)")
// Base64 Decode (go back the other way)
// Notice the unwrapping given the NSString! optional
// NSData returned
let data = NSData(base64EncodedString: base64Encoded, options: NSDataBase64DecodingOptions.fromRaw(0)!)
// Convert back to a string
let base64Decoded = NSString(data: data, encoding: NSUTF8StringEncoding)
println("Decoded: \(base64Decoded)")
裁判:http://iosdevelopertips.com/swift-code/base64-encode-decode-swift.html
斯威夫特擴展是清潔和便利。 https://stackoverflow.com/a/46969102/1996294 – 2017-11-03 08:59:01