stack0101
stack0101

Reputation: 3

I got an error swift on xcode7.

That error tells me that "Result value in ‘?’ :’ expression have mismatching types ’NSJONWritingOptions’ and ‘_'". Does anyone know how to fix this? I wrote these codes on xcode6.3.1 and converted to xcode7 just now. worked on xcode6 though....

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!"
    }
}

Upvotes: 0

Views: 206

Answers (2)

Shripada
Shripada

Reputation: 6514

I use this in my Swift 2.0 implementations-

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

Upvotes: 0

Sebastian Osiński
Sebastian Osiński

Reputation: 3004

options in NSJSONSerialization.dataWithJSONObject:options: should be an empty array if you don't want to specify any options. So your code should look like this:

let opts = pretty ? NSJSONWritingOptions.PrettyPrinted : []

Previously it expected nil, but there was change in the way iOS SDK is mapped in Swift.

Upvotes: 1

Related Questions