我在xcode7上得到了一个错误。

时间:2016-01-12 08:38:37

标签: swift xcode7

该错误告诉我"'?':'表达式中的结果值具有不匹配的类型'NSJONWritingOptions'和'_'"。有谁知道如何解决这一问题?我在xcode6.3.1上编写了这些代码,刚才转换为xcode7。曾经在xcode6上工作....

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.longLongValue.description
        case "Q", "L", "I", "S":
            return o.unsignedLongLongValue.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
           //below is the code I got an error for
            ? NSJSONWritingOptions.PrettyPrinted : nil
        if let data = (try? NSJSONSerialization.dataWithJSONObject(
            _value, options:opts)) as NSData? {
                if let result = NSString(
                    data:data, encoding:NSUTF8StringEncoding
                    ) as? String {
                        return result
                }
        }
        return "YOU ARE NOT SUPPOSED TO SEE THIS!"
    }
}

2 个答案:

答案 0 :(得分:1)

如果您不想指定任何选项,则options中的

NSJSONSerialization.dataWithJSONObject:options:应为空数组。所以你的代码应该是这样的:

let opts = pretty ? NSJSONWritingOptions.PrettyPrinted : []

之前预计nil,但iOS SDK在Swift中的映射方式发生了变化。

答案 1 :(得分:0)

我在Swift 2.0实现中使用它 -

let options = prettyPrinted ? 
         NSJSONWritingOptions.PrettyPrinted : NSJSONWritingOptions(rawValue: 0)