我在xcode7上遇到了一个错误迅速。

I got an error swift on xcode7.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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!"
    }
}


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

options应该是一个空数组。因此,您的代码应如下所示:

1
let opts = pretty ? NSJSONWritingOptions.PrettyPrinted : []

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


我在Swift 2.0实现中使用了它-

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