swift - 我在 xcode7 上很快遇到错误。

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

最佳答案

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

options 应该是一个空数组。所以你的代码应该是这样的:

let opts = pretty ? NSJSONWritingOptions.PrettyPrinted : []

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

关于swift - 我在 xcode7 上很快遇到错误。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34738929/

相关文章:

ios - 单个 IBAction 和 IBOutlet 的 SIGABRT 错误

ios - 快速删除表格 View 中的一行

ios - Xcode 7 GM Show Segue 变成模态 Segue

crash - Xcode 7 UITextView 奇怪的行为导致 View 加载失败

swift - 在没有 iOS 设备的 Apple Watch 上安装 Apple Watch OS 2 应用程序

ios - 为什么我的 UICollectionView 在单击单元格时会向上滚动?

ios - 为选择 View Controller 创建导航栏标题图像会产生奇怪的行为

ios - 将 UITabBarDelegate 设置为 UIViewController 时应用程序崩溃

swift - 使用 NSCollectionView 设置标题标题

ios - 在没有模拟器的情况下在xcode 7中编译 objective-c