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

base64EncodedStringWithOptions in Swift fails with compile error(Swift 中的 base64EncodedStringWithOptions 因编译错误而失败)
本文介绍了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 因编译错误而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)