Swift 中的 base64EncodedStringWithOptions 因编译错误而失败 [英] base64EncodedStringWithOptions in Swift fails with compile error

查看:43
本文介绍了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 因编译错误而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆