如何解码Swift5版本?
How to decode Swift 5 version?
/ Decoding JSON array with codable - Swift 5
我是参考这两篇文章尝试做的。但这与我正在尝试做的非常不同。
我引用的其他代码
let base64decryptData = NSMutableData(base64EncodedString: encodeBase64String, options: NSDataBase64DecodingOptions.allZeros)
let decodeBase64String: NSString = NSString(data: base64decryptData!, encoding: NSUTF8StringEncoding)!
NSLog("Base64 decoding : %@", decodeBase64String)
这与我正在尝试做的最相似,但我不知道语法是否有很大变化,因为它是 2015 年的文本,或者我是否找到了错误的解决方案,所以我问问题。
总而言之,我想把图片这样的代码解码成base64,作为简洁的代码使用。如果你能告诉我如何做,我将不胜感激(这是非常 little of the code
的捕获。)
我的代码
if let httpResponse = response as? HTTPURLResponse {
print( httpResponse.allHeaderFields )
guard let data = data else {return}
let decoder = JSONDecoder()
struct UserData: Codable {
let userId: String?
let profile: String?
}
do {
let userData = try decoder.decode(UserData.self, from: data)
DispatchQueue.main.async {
let ad = UIApplication.shared.delegate as? AppDelegate
ad?.paramProfile = userData.profile
let base64decryptData = NSMutableData(base64EncodedString: ad?.paramProfile ?? "", options: NSData.Base64DecodingOptions) // ERROR[Cannot convert value of type 'NSData.Base64DecodingOptions.Type' to expected argument type 'NSData.Base64DecodingOptions']
let decodeBase64String: NSString = NSString(data: base64decryptData!, encoding: NSUTF8StringEncoding)!
print("AppDelegate - paramProfile : ",decodeBase64String )
}
} catch {
print("Error:\(error)")
}
}
AppDelegate
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var paramId: String?
var paramProfile: String?
代码说明:创建随机值UserData
后,与AppDelegate
中的值连接后转为data64
不要使用 NSString。不要使用 NSMutableData。不要使用 NSLog。这是 Swift,而不是 Objective-C。使用数据。
解码经过 Base 64 编码的字符串的示例:
let s = "d2VsbCBob3dkeSB0aGVyZQ=="
let d = Data(base64Encoded: s)!
let s2 = String(data: d, encoding: .utf8)!
print(s2) // well howdy there
这可能与您正在尝试做的事情不同(我无法弄清楚您正在尝试做什么),但它应该可以帮助您入门。只需查阅有关数据的文档。
我是参考这两篇文章尝试做的。但这与我正在尝试做的非常不同。
我引用的其他代码
let base64decryptData = NSMutableData(base64EncodedString: encodeBase64String, options: NSDataBase64DecodingOptions.allZeros)
let decodeBase64String: NSString = NSString(data: base64decryptData!, encoding: NSUTF8StringEncoding)!
NSLog("Base64 decoding : %@", decodeBase64String)
这与我正在尝试做的最相似,但我不知道语法是否有很大变化,因为它是 2015 年的文本,或者我是否找到了错误的解决方案,所以我问问题。
总而言之,我想把图片这样的代码解码成base64,作为简洁的代码使用。如果你能告诉我如何做,我将不胜感激(这是非常 little of the code
的捕获。)
我的代码
if let httpResponse = response as? HTTPURLResponse {
print( httpResponse.allHeaderFields )
guard let data = data else {return}
let decoder = JSONDecoder()
struct UserData: Codable {
let userId: String?
let profile: String?
}
do {
let userData = try decoder.decode(UserData.self, from: data)
DispatchQueue.main.async {
let ad = UIApplication.shared.delegate as? AppDelegate
ad?.paramProfile = userData.profile
let base64decryptData = NSMutableData(base64EncodedString: ad?.paramProfile ?? "", options: NSData.Base64DecodingOptions) // ERROR[Cannot convert value of type 'NSData.Base64DecodingOptions.Type' to expected argument type 'NSData.Base64DecodingOptions']
let decodeBase64String: NSString = NSString(data: base64decryptData!, encoding: NSUTF8StringEncoding)!
print("AppDelegate - paramProfile : ",decodeBase64String )
}
} catch {
print("Error:\(error)")
}
}
AppDelegate
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var paramId: String?
var paramProfile: String?
代码说明:创建随机值UserData
后,与AppDelegate
data64
不要使用 NSString。不要使用 NSMutableData。不要使用 NSLog。这是 Swift,而不是 Objective-C。使用数据。
解码经过 Base 64 编码的字符串的示例:
let s = "d2VsbCBob3dkeSB0aGVyZQ=="
let d = Data(base64Encoded: s)!
let s2 = String(data: d, encoding: .utf8)!
print(s2) // well howdy there
这可能与您正在尝试做的事情不同(我无法弄清楚您正在尝试做什么),但它应该可以帮助您入门。只需查阅有关数据的文档。