I've NSData
category for base64Encoding
written in Obj-C.
@interface NSData (Base64)
+ (NSData*)dataFromBase64String:(NSString*)aString;
- (NSString*)base64EncodedString;
@end
Initially in Swift 2.2, I was using it as:
let dbPassData = NSData(fromBase64String: password)
Now, in Swift 3, there is no any NSData
, instead have Data
.
How to use these category methods in swift 3?
After converting project to Swift 3, it converted that line as:
let dbPassData = Data(fromBase64String: password)
which yields an error:
Argument labels do not match any available overloads
In Swift 3, Apple also renamed some initializers and Xcode 8 beta makes some mistakes when converting from 2.2 to 3.
In that case, the initializer was renamed to this:
let data = Data(base64Encoded: "hello")
When you are facing this or similar problems, you can start typing ClassName(
and look at the proposed initializers. In most cases, you'll find the new one easily.
NSData
is still available in Swift 3, just like String
has been coexisting with NSString
. You need to ignore the compiler's advice in this case:
let dbPassData = NSData(fromBase64String: password) as Data
This assumes you don't want to port your Base64
category to Swift or you want to maintain compatibility with Obj-C code. Swift 3's Data
also offer built-in Base 64 conversion:
let dbPassData = Data(base64Encoded: password)
let base64String = data.base64EncodedString(options: [.lineLength64Characters, .endLineWithLineFeed])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.