[英]Accessing NS Type class category in swift-3
I've NSData
category for base64Encoding
written in Obj-C. 我用Obj-C编写的
base64Encoding
NSData
类别。
@interface NSData (Base64)
+ (NSData*)dataFromBase64String:(NSString*)aString;
- (NSString*)base64EncodedString;
@end
Initially in Swift 2.2, I was using it as: 最初在Swift 2.2中,我使用它作为:
let dbPassData = NSData(fromBase64String: password)
Now, in Swift 3, there is no any NSData
, instead have Data
. 现在,在Swift 3中,没有任何
NSData
,而是拥有Data
。
How to use these category methods in swift 3? 如何在swift 3中使用这些类别方法?
After converting project to Swift 3, it converted that line as: 将项目转换为Swift 3后,它将该行转换为:
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. 在Swift 3中,Apple还重命名了一些初始化程序,而Xcode 8 beta在从2.2转换为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. 当您遇到此类问题或类似问题时,您可以开始键入
ClassName(
并查看建议的初始值设定项。在大多数情况下,您可以轻松找到新的初始值。
NSData
is still available in Swift 3, just like String
has been coexisting with NSString
. NSData
仍然可以在Swift 3中使用,就像String
已经与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. 这假设您不希望将
Base64
类别移植到Swift,或者您希望保持与Obj-C代码的兼容性。 Swift 3's Data
also offer built-in Base 64 conversion: Swift 3的
Data
还提供内置的Base 64转换:
let dbPassData = Data(base64Encoded: password)
let base64String = data.base64EncodedString(options: [.lineLength64Characters, .endLineWithLineFeed])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.