问题描述
雨燕1.2
let apiLoginString = NSString(format: "%@:%@", API_USERNAME, API_PASSWORD);
let apiLoginData: NSData = apiLoginString.dataUsingEncoding(NSUTF8StringEncoding)!;
var base64ApiLoginString = apiLoginData.base64EncodedStringWithOptions(nil);
雨燕2
let apiLoginString = NSString(format: "%@:%@", API_USERNAME, API_PASSWORD);
let apiLoginData: NSData = apiLoginString.dataUsingEncoding(NSUTF8StringEncoding)!;
var base64ApiLoginString = apiLoginData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions());
这两种方法产生不同的结果.在Swift 2中,我们无法再传入nil
;
apiLoginData.base64EncodedStringWithOptions(nil);
我也尝试在Swift 2中传递NSDataBase64EncodingOptions.Encoding64CharacterLineLength
,但是结果仍然不同
有什么想法吗?
更新2:Swift 1.2或Swift 2都不是问题.奇怪的是,这2打印出了不同的结果. 更新3:结果就像马丁指出的那样,我的API_USERNAME变量中有一些不可见的字符 类似于 Swift 2.0日历组件错误或 Swift 2.0-二进制运算符"|"不能应用于两个UIUserNotificationType操作数, 因此有效的选项例如: 完整示例: Swift 1.2 Swift 2 Those 2 approach yield different results. In Swift 2 we cannot pass in I have also tried passed in Any thoughts? UPDATE 2: It is not the problem with Swift 1.2 or Swift 2. Oddly enough those 2 prints out different results.. What the F!! UPDATE 3: Turns out that as Martin pointed out, I have some invisible character in my API_USERNAME variable Similarly as in Swift 2.0 calendar components error or Swift 2.0 - Binary Operator "|" cannot be applied to two UIUserNotificationType operands, So valid options would be for example: Full example: 这篇关于Swift 2 base64编码产生不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!class RequestHelper: NSObject {
static var API_USERNAME: NSString = "abc";
static var API_PASSWORD: NSString = "123";
static var USERNAME: NSString = "abc"
static var PASSWORD: NSString = "123"
class func signUpUser() {
//Base64
var loginString = NSString(format: "%@:%@", USERNAME, PASSWORD);
var loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!;
var base64LoginString = loginData.base64EncodedStringWithOptions([]);
print("BASE 64 \(base64LoginString)");//RESULT YWJjOjEyMw==
let apiLoginString = NSString(format: "%@:%@", API_USERNAME, API_PASSWORD);
let apiLoginData: NSData = apiLoginString.dataUsingEncoding(NSUTF8StringEncoding)!;
var base64ApiLoginString = apiLoginData.base64EncodedStringWithOptions([]);
print("BASE 64 \(base64ApiLoginString)");// RESULT IS 4oGg4oGg4oGgYWJjOjEyMw==
}
NSDataBase64EncodingOptions
是Swift 2中的OptionSetType
.它提供了一个类似于set的接口,可以从数组中进行初始化文字.base64EncodedStringWithOptions([]) // no options
base64EncodedStringWithOptions([.Encoding64CharacterLineLength])
base64EncodedStringWithOptions([.Encoding64CharacterLineLength, .EncodingEndLineWithCarriageReturn])
let apiLoginString = NSString(format: "%@:%@", "user", "password")
let apiLoginData = apiLoginString.dataUsingEncoding(NSUTF8StringEncoding)!
let base64ApiLoginString = apiLoginData.base64EncodedStringWithOptions([])
print(base64ApiLoginString)
// dXNlcjpwYXNzd29yZA==
let apiLoginString = NSString(format: "%@:%@", API_USERNAME, API_PASSWORD);
let apiLoginData: NSData = apiLoginString.dataUsingEncoding(NSUTF8StringEncoding)!;
var base64ApiLoginString = apiLoginData.base64EncodedStringWithOptions(nil);
let apiLoginString = NSString(format: "%@:%@", API_USERNAME, API_PASSWORD);
let apiLoginData: NSData = apiLoginString.dataUsingEncoding(NSUTF8StringEncoding)!;
var base64ApiLoginString = apiLoginData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions());
nil
anymore;apiLoginData.base64EncodedStringWithOptions(nil);
NSDataBase64EncodingOptions.Encoding64CharacterLineLength
in Swift 2. But the results is still differemtclass RequestHelper: NSObject {
static var API_USERNAME: NSString = "abc";
static var API_PASSWORD: NSString = "123";
static var USERNAME: NSString = "abc"
static var PASSWORD: NSString = "123"
class func signUpUser() {
//Base64
var loginString = NSString(format: "%@:%@", USERNAME, PASSWORD);
var loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)!;
var base64LoginString = loginData.base64EncodedStringWithOptions([]);
print("BASE 64 \(base64LoginString)");//RESULT YWJjOjEyMw==
let apiLoginString = NSString(format: "%@:%@", API_USERNAME, API_PASSWORD);
let apiLoginData: NSData = apiLoginString.dataUsingEncoding(NSUTF8StringEncoding)!;
var base64ApiLoginString = apiLoginData.base64EncodedStringWithOptions([]);
print("BASE 64 \(base64ApiLoginString)");// RESULT IS 4oGg4oGg4oGgYWJjOjEyMw==
}
NSDataBase64EncodingOptions
is an OptionSetType
in Swift 2.It offers a set-like interface and can be initialized from an arrayliteral.base64EncodedStringWithOptions([]) // no options
base64EncodedStringWithOptions([.Encoding64CharacterLineLength])
base64EncodedStringWithOptions([.Encoding64CharacterLineLength, .EncodingEndLineWithCarriageReturn])
let apiLoginString = NSString(format: "%@:%@", "user", "password")
let apiLoginData = apiLoginString.dataUsingEncoding(NSUTF8StringEncoding)!
let base64ApiLoginString = apiLoginData.base64EncodedStringWithOptions([])
print(base64ApiLoginString)
// dXNlcjpwYXNzd29yZA==