Ios 如何解码Swift 5版本?
/ 我试图通过参考这两篇文章来做到这一点。但这和我现在想做的很不一样 我引用的其他代码Ios 如何解码Swift 5版本?,ios,swift,decode,appdelegate,Ios,Swift,Decode,Appdelegate,/ 我试图通过参考这两篇文章来做到这一点。但这和我现在想做的很不一样 我引用的其他代码 let base64decryptData = NSMutableData(base64EncodedString: encodeBase64String, options: NSDataBase64DecodingOptions.allZeros) let decodeBase64String: NSString = NSString(data: base64decryptData!, encoding:
let base64decryptData = NSMutableData(base64EncodedString: encodeBase64String, options: NSDataBase64DecodingOptions.allZeros)
let decodeBase64String: NSString = NSString(data: base64decryptData!, encoding: NSUTF8StringEncoding)!
NSLog("Base64 decoding : %@", decodeBase64String)
这与我想做的最为相似,但我不知道语法是否因为是2015年的文本而发生了很大变化,或者我是否找到了错误的解决方案,所以我要问一个问题
总而言之,我想将图片中的代码解码为base64,并将其用作简明代码。如果您能告诉我如何操作,我将不胜感激(这是一个非常小部分代码的捕获
)
mycode
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)")
}
}
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var paramId: String?
var paramProfile: String?
AppDelegate
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)")
}
}
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var paramId: String?
var paramProfile: String?
代码说明:在创建随机值
UserData
后,在与AppDelegate
中的值连接后,将其转换为data64
,不要使用NSString。不要使用NSMutableData。不要使用NSLog。这是快速的,不是客观的。使用数据
解码已按Base 64编码的字符串的示例:
let s = "d2VsbCBob3dkeSB0aGVyZQ=="
let d = Data(base64Encoded: s)!
let s2 = String(data: d, encoding: .utf8)!
print(s2) // well howdy there
这可能和你想做的不一样(我不知道你想做什么),但它应该让你开始。请查阅有关数据的文档。我将从一开始就解释问题的内容。我正在使用urlsession与服务器通信。我想将服务器提供的长字符值更改为base64解码,并将其更改为视觉上可识别的值。这是对您刚才留下的评论的回答。首先,我将参考苹果文档,按照您告诉我的方式进行更改。谢谢你,但你如何做到这一点取决于编码的确切内容。错误很明显。您需要传递枚举选项,而不是其类型
NSMutableData(base64Encoded:,选项:。ignoreUnknownCharacters)
且初始值设定项参数已重命名为base64Encoded
,但如Matt所述,在使用Swift编码时,应使用Swift本机类型。只是不要强行打开结果