<i id='cKtkj'><tr id='cKtkj'><dt id='cKtkj'><q id='cKtkj'><span id='cKtkj'><b id='cKtkj'><form id='cKtkj'><ins id='cKtkj'></ins><ul id='cKtkj'></ul><sub id='cKtkj'></sub></form><legend id='cKtkj'></legend><bdo id='cKtkj'><pre id='cKtkj'><center id='cKtkj'></center></pre></bdo></b><th id='cKtkj'></th></span></q></dt></tr></i><div id='cKtkj'><tfoot id='cKtkj'></tfoot><dl id='cKtkj'><fieldset id='cKtkj'></fieldset></dl></div>

<small id='cKtkj'></small><noframes id='cKtkj'>

        <bdo id='cKtkj'></bdo><ul id='cKtkj'></ul>

    1. <legend id='cKtkj'><style id='cKtkj'><dir id='cKtkj'><q id='cKtkj'></q></dir></style></legend>

      <tfoot id='cKtkj'></tfoot>

        如何在 Swift 中将字符串编码为 Base64?

        时间:2024-04-14
      1. <i id='nEgtj'><tr id='nEgtj'><dt id='nEgtj'><q id='nEgtj'><span id='nEgtj'><b id='nEgtj'><form id='nEgtj'><ins id='nEgtj'></ins><ul id='nEgtj'></ul><sub id='nEgtj'></sub></form><legend id='nEgtj'></legend><bdo id='nEgtj'><pre id='nEgtj'><center id='nEgtj'></center></pre></bdo></b><th id='nEgtj'></th></span></q></dt></tr></i><div id='nEgtj'><tfoot id='nEgtj'></tfoot><dl id='nEgtj'><fieldset id='nEgtj'></fieldset></dl></div>

              <tbody id='nEgtj'></tbody>
            • <bdo id='nEgtj'></bdo><ul id='nEgtj'></ul>

                <tfoot id='nEgtj'></tfoot>

                <small id='nEgtj'></small><noframes id='nEgtj'>

                <legend id='nEgtj'><style id='nEgtj'><dir id='nEgtj'><q id='nEgtj'></q></dir></style></legend>
                • 本文介绍了如何在 Swift 中将字符串编码为 Base64?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想将字符串转换为 Base64.我在几个地方找到了答案,但它在 Swift 中不再起作用.我正在使用 Xcode 6.2.我相信答案可能适用于以前的 Xcode 版本,而不是 Xcode 6.2.

                  I want to convert a string to Base64. I found answers 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 versions and not Xcode 6.2.

                  有人可以指导我在 Xcode 6.2 中执行此操作吗?

                  Could someone please guide me to do this in Xcode 6.2?

                  我找到的答案是这样的,但它在我的 Xcode 版本中不起作用:

                  The answer I found was this, but it does not work in my version of Xcode:

                  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

                  推荐答案

                  我没有安装 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:).有点令人惊讶的是,这不是一个可失败的初始化程序,因此您不需要解包它.

                  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(base64EncodedString:) 一个可失败的初始化器,你需要解开它.

                  But since NSData(base64EncodedString:) is a failable initializer, you need to unwrap that.

                  顺便说一句,所有这些更改都是由 Xcode 迁移器建议的(单击排水沟中的错误消息,它有一个修复"建议).

                  Btw, all these changes were suggested by Xcode migrator (click the error message in the gutter and it has a "fix-it" suggestion).

                  为避免强制解包而重写的最终代码如下所示:

                  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)")
                      }
                  }
                  

                  (如果使用 Swift 1.2,你可以使用多个 if-let 而不是 map)

                  (if using Swift 1.2 you could use multiple if-lets instead of the map)

                  Swift 5 更新:

                  import Foundation
                  
                  let str = "iOS Developer Tips encoded in Base64"
                  print("Original: (str)")
                  
                  let utf8str = str.data(using: .utf8)
                  
                  if let base64Encoded = utf8str?.base64EncodedString(options: Data.Base64EncodingOptions(rawValue: 0)) {
                      print("Encoded: (base64Encoded)")
                  
                      if let base64Decoded = Data(base64Encoded: base64Encoded, options: Data.Base64DecodingOptions(rawValue: 0))
                      .map({ String(data: $0, encoding: .utf8) }) {
                          // Convert back to a string
                          print("Decoded: (base64Decoded ?? "")")
                      }
                  }
                  

                  这篇关于如何在 Swift 中将字符串编码为 Base64?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Android中base64字符串编码和解码位图对象 下一篇:如何在 Swift 中将 base64String 转换为 String?

                  相关文章

                • <small id='r5VBA'></small><noframes id='r5VBA'>

                    1. <tfoot id='r5VBA'></tfoot>
                    2. <i id='r5VBA'><tr id='r5VBA'><dt id='r5VBA'><q id='r5VBA'><span id='r5VBA'><b id='r5VBA'><form id='r5VBA'><ins id='r5VBA'></ins><ul id='r5VBA'></ul><sub id='r5VBA'></sub></form><legend id='r5VBA'></legend><bdo id='r5VBA'><pre id='r5VBA'><center id='r5VBA'></center></pre></bdo></b><th id='r5VBA'></th></span></q></dt></tr></i><div id='r5VBA'><tfoot id='r5VBA'></tfoot><dl id='r5VBA'><fieldset id='r5VBA'></fieldset></dl></div>

                    3. <legend id='r5VBA'><style id='r5VBA'><dir id='r5VBA'><q id='r5VBA'></q></dir></style></legend>
                        <bdo id='r5VBA'></bdo><ul id='r5VBA'></ul>