Swift 2到3迁移String.fromCString

Swift 2到3迁移String.fromCString,第1张

概述我正在尝试将以下代码从此库( https://github.com/dankogai/swift-json)转换为Swift 3兼容代码. 关于fromCString函数存在大量错误,这些错误以前在Swift 2中的String上可用 到处都是错误大致相同: ‘fromCString’ is unavailable: Please use String.init?(validatingUTF8:) 我正在尝试将以下代码从此库( https://github.com/dankogai/swift-json)转换为Swift 3兼容代码.

关于fromCString函数存在大量错误,这些错误以前在Swift 2中的String上可用

到处都是错误大致相同:

‘fromCString’ is unavailable: Please use String.init?(valIDatingUTF8:) instead. Note that it no longer accepts NulL as a valID input. Also consIDer using String(cString:),that will attempt to repair ill-formed code units.

因为我有两个选择可供选择我不确定给出原作者意图的正确选择.

例如,这是一个带错误的片段.

extension JsON : customstringconvertible {    /// stringifIEs self.    /// if pretty:true it pretty prints    public func toString(pretty:Bool=false)->String {        switch _value {        case is NSError: return "\(_value)"        case is NSNull: return "null"        case let o as NSNumber:            switch String.fromCString(o.objCType)! {            case "c","C":                return o.boolValue.description            case "q","l","i","s":                return o.int64Value.description            case "Q","L","I","S":                return o.uint64Value.description            default:                switch o.doubleValue {                case 0.0/0.0:   return "0.0/0.0"    // NaN                case -1.0/0.0:  return "-1.0/0.0"   // -infinity                case +1.0/0.0:  return "+1.0/0.0"   //  infinity                default:                    return o.doubleValue.description                }            }        case let o as Nsstring:            return o.deBUGDescription        default:            let opts = pretty ? JsONSerialization.WritingOptions.prettyPrinted : JsONSerialization.WritingOptions()            if let data = (try? JsONSerialization.data(                withJsONObject: _value,options:opts)) as NSData? {                    if let result = Nsstring(                        data:data as Data,enCoding:String.EnCoding.utf8.rawValue                        ) as? String {                            return result                    }            }            return "YOU ARE NOT SUPPOSED TO SEE THIS!"        }    }    public var description:String { return toString() }}

请注意上面代码中的fromCString.正确模拟Swift 2行为的正确方法是什么?

因为错误建议像这样使用init(cString :).
String(cString:o.objCType)

检查Apple Documentation以获取更多详细信息.

总结

以上是内存溢出为你收集整理的Swift 2到3迁移String.fromCString全部内容,希望文章能够帮你解决Swift 2到3迁移String.fromCString所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1089557.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存