ios Swift 中的 base64EncodedStringWithOptions 因编译错误而失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24769074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:24:21  来源:igfitidea点击:

base64EncodedStringWithOptions in Swift fails with compile error

iosswiftbase64

提问by Mikhail Larionov

let dataStr = data.base64EncodedStringWithOptions(options: Encoding64CharacterLineLength)

Doesn't compile with "Use of unresolved identifier 'Encoding64CharacterLineLength'" When I just change the param to zero with

不编译“使用未解析的标识符'Encoding64CharacterLineLength'”当我只是将参数更改为零时

let dataStr = data.base64EncodedStringWithOptions(options: 0)

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.

它给出了更奇怪的错误:“无法转换类型为 'String!' 的表达式!输入'String!'”我找到了一种用NSData初始化NSString的方法(但是,我仍然无法区分String和NSString之间的区别),但我真的很好奇为什么这两行代码不起作用。

回答by slazyk

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.

除非明确给出外部名称,否则 Swift 中方法的第一个参数不是命名参数。因此你应该做的是:data.base64EncodedStringWithOptions(x)没有options:零件。

If you actually look at the argument type, NSDataBase64EncodingOptions, you'll notice that it is a struct conforming to RawOptionSetwith static variables for option constants. Therefore to use them you should do: NSDataBase64EncodingOptions.Encoding64CharacterLineLength

如果您实际查看参数类型 ,NSDataBase64EncodingOptions您会注意到它是一个符合RawOptionSet选项常量的静态变量的结构。因此,要使用它们,您应该执行以下操作:NSDataBase64EncodingOptions.Encoding64CharacterLineLength

The NSDataBase64EncodingOptionsstruct (or RawOptionSetin general) is also not convertible from integer literals (like 0). But it does conform to NilLiteralConvertibleso if you don't want any options you can pass nil.

所述NSDataBase64EncodingOptions结构(或RawOptionSet一般的)也不会从整数常量(如可转换0)。但它确实符合,NilLiteralConvertible所以如果你不想要任何选项,你可以通过nil

Putting it together:

把它放在一起:

let dataStr = data.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)

or

或者

let dataStr = data.base64EncodedStringWithOptions(nil)

Swift3.0

斯威夫特3.0

let dataStr = data.base64EncodedString(options: [])

回答by zaph

For Swift 2.x use an array for options:

对于 Swift 2.x,使用数组作为选项:

let dataStr = data.base64EncodedStringWithOptions([.Encoding64CharacterLineLength])
let dataStr = data.base64EncodedStringWithOptions([])

回答by Kiran P Nair

For swift 3.0 use this ,

对于 swift 3.0 使用这个,

var dataStr = data.base64EncodedString(options: .lineLength64Characters)

回答by Hemang

Swift 3.x

斯威夫特 3.x

let fileStream = fileData?.base64EncodedString(options: NSData.Base64EncodingOptions.init(rawValue: 0))

回答by Sean McLaughlin

Since the default parameter value is set be an empty array…

由于默认参数值设置为空数组......

/// Returns a Base-64 encoded string.
///
/// - parameter options: The options to use for the encoding. Default value is `[]`.
/// - returns: The Base-64 encoded string.
@inlinable public func base64EncodedString(options: Data.Base64EncodingOptions = []) -> String

you just need to call

你只需要打电话

let dataStr = data.base64EncodedString()

回答by Mike Manh

You don't have to put in the "options:" identifier in the argument. You DO have to specify that Encoding64CharacterLineLength is a static member of NSDataBase64EncodingOptions, like so:

您不必在参数中放入“选项:”标识符。您必须指定 Encoding64CharacterLineLength 是 NSDataBase64EncodingOptions 的静态成员,如下所示:

var dataStr = data.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)

回答by sabes

let dataStr = data.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.allZeros)