一直试图让它在 Swift 2.0 中工作,错误提示:
Type NSJSONWritingOptions cannot conform to protocol NilLiteralConvertible
在var options = PrettyPrinted...
:
func JSONStringify(value: AnyObject,prettyPrinted:Bool = false) -> String {
var options = prettyPrinted ? NSJSONWritingOptions.PrettyPrinted : nil
if NSJSONSerialization.isValidJSONObject(value) {
do{
let data = try NSJSONSerialization.dataWithJSONObject(value, options: options)
if let string = NSString(data: data, encoding: NSUTF8StringEncoding) {
return string as String
}
} catch {
}
}
return ""
}
最佳答案
let options = prettyPrinted ?
NSJSONWritingOptions.PrettyPrinted : NSJSONWritingOptions(rawValue: 0)
是 swift 2.0 的正确语法
关于swift2 - 在 Swift 2 中使用 NSSerialization.datawithJSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30920766/