base64EncodedStringWithOptions in Swift fails with compile error(Swift 中的 base64EncodedStringWithOptions 因编译错误而失败)
问题描述
let dataStr = data.base64EncodedStringWithOptions(options: Encoding64CharacterLineLength)
不使用使用未解析的标识符 'Encoding64CharacterLineLength'"进行编译当我只是将参数更改为零时
Doesn't compile with "Use of unresolved identifier 'Encoding64CharacterLineLength'" When I just change the param to zero with
let dataStr = data.base64EncodedStringWithOptions(options: 0)
它给出了更奇怪的错误:无法转换'String!'类型的表达式!键入'String!'"我找到了一种使用 NSData 初始化 NSString 的方法(但是,我仍然无法区分 String 和 NSString),但我真的很好奇为什么这两行代码不起作用.
It gives even stranger error: "Cannot convert the expression of type 'String!' to type 'String!'" I found a way to init NSString with NSData (however, I still can't get the difference between String and NSString), but I'm really curious why these two lines of code don't work.
推荐答案
除非明确给出外部名称,否则 Swift 中方法的第一个参数不是命名参数.因此,您应该这样做: data.base64EncodedStringWithOptions(x)
没有 options:
部分.
Unless explicitly given an external name, first argument of a method in Swift is not a named argument. Therefore you should be doing: data.base64EncodedStringWithOptions(x)
without the options:
part.
如果您实际查看参数类型 NSDataBase64EncodingOptions
,您会注意到它是一个符合 RawOptionSet
的结构,其中带有用于选项常量的静态变量.因此,要使用它们,您应该这样做: NSDataBase64EncodingOptions.Encoding64CharacterLineLength
If you actually look at the argument type, NSDataBase64EncodingOptions
, you'll notice that it is a struct conforming to RawOptionSet
with static variables for option constants. Therefore to use them you should do: NSDataBase64EncodingOptions.Encoding64CharacterLineLength
NSDataBase64EncodingOptions
结构(或一般的 RawOptionSet
)也不能从整数文字转换(如 0
).但它确实符合NilLiteralConvertible
,所以如果你不想要任何选项,你可以传递nil
.
The NSDataBase64EncodingOptions
struct (or RawOptionSet
in general) is also not convertible from integer literals (like 0
). But it does conform to NilLiteralConvertible
so if you don't want any options you can pass nil
.
把它放在一起:
let dataStr = data.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
或
let dataStr = data.base64EncodedStringWithOptions(nil)
Swift3.0
let dataStr = data.base64EncodedString(options: [])
这篇关于Swift 中的 base64EncodedStringWithOptions 因编译错误而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Swift 中的 base64EncodedStringWithOptions 因编译错误而失败


- Android - 拆分 Drawable 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- android 4中的android RadioButton问题 2022-01-01