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 因编译错误而失败
基础教程推荐
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
