问题描述
我想一个字符串转换为一个base64。我发现同样的答案在几个地方,但它不能在迅速再工作。我用X code 6.2。我相信答案可能是previous X code工件不是X code 6.2。
I want to convert a string to a base64. I found same answer in several places but it does not work anymore in swift. I am using Xcode 6.2. I believe the answer might be work in previous Xcode not Xcode 6.2.
可能有人请引导我做这个以x code 6.2?
Could someone please guide me to do this in Xcode 6.2 ?
我找到了答案是这样的。但它并没有在X code工件6.2
The answer I found was this. but it does not work in 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)")
REF:<一href=\"http://iosdevelopertips.com/swift-$c$c/base64-en$c$c-de$c$c-swift.html\">http://iosdevelopertips.com/swift-$c$c/base64-en$c$c-de$c$c-swift.html
推荐答案
我没有安装6.2,但我不认为是6.3在这方面有什么不同:
I don’t have 6.2 installed but I don’t think 6.3 is any different in this regard:
dataUsingEncoding
返回一个可选的,所以你需要解开了。
dataUsingEncoding
returns an optional, so you need to unwrap that.
NSDataBase64EncodingOptions.fromRaw
已被替换 NSDataBase64EncodingOptions(rawValue:)
。稍微出人意料的是,这不是一个failable初始化,所以你并不需要解开它。
NSDataBase64EncodingOptions.fromRaw
has been replaced with NSDataBase64EncodingOptions(rawValue:)
. Slightly surprisingly, this is not a failable initializer so you don’t need to unwrap it.
不过,由于的NSData(base64En codedString:)
的是的一个failable初始化,需要解开了
But since NSData(base64EncodedString:)
is a failable initializer, you need to unwrap that.
顺便说一句,所有这些变化进行了X code迁移建议(请在阴沟里的错误信息,它有一个修复的建议)。
Btw, all these changes were suggested by Xcode migrator (click the error message in the gutter and it has a "fix-it" suggestion).
最后code,改写避免力解包,看起来是这样的:
Final code, rewritten to avoid force-unwraps, looks like this:
import Foundation
let str = "iOS Developer Tips encoded in Base64"
println("Original: \(str)")
let utf8str = str.dataUsingEncoding(NSUTF8StringEncoding)
if let base64Encoded = utf8str?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
{
println("Encoded: \(base64Encoded)")
if let base64Decoded = NSData(base64EncodedString: base64Encoded, options: NSDataBase64DecodingOptions(rawValue: 0))
.map({ NSString(data: $0, encoding: NSUTF8StringEncoding) })
{
// Convert back to a string
println("Decoded: \(base64Decoded)")
}
}
(如果使用雨燕1.2,您可以使用多个if-可以代替图)
(if using Swift 1.2 you could use multiple if-lets instead of the map)
这篇关于如何连接code字符串在迅速采用base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!