ios - 尝试以 swift 2 重载错误的形式读取 JSON 文件

标签 ios json xcode swift

您好,这是在 Swift 2 中解析 JSON 文件的样板代码。我以前用过它并且它可以工作,但由于某种原因它在最新版本的 Xcode 7.1 和 Swift 2 中损坏了。对这些人有什么想法吗?错误是“参数标签‘(contentsOfFile:, options:, error:)’不匹配任何可用的重载”

import Foundation

extension Dictionary {
    static func loadJSONFromBundle(filename: String) -> Dictionary<String, AnyObject>? {
        if let path = NSBundle.mainBundle().pathForResource(filename, ofType: "json") {

            var error: NSError?
            let data = NSData(contentsOfFile: path, options: NSDataReadingOptions, error: &error)
            if let data = data {
                let dictionary: AnyObject? = NSJSONSerialization.JSONObjectWithData(data,
                    options: NSJSONReadingOptions(), error: &error)
                if let dictionary = dictionary as? Dictionary<String, AnyObject> {
                    return dictionary
                } else {
                    print("Level file '\(filename)' is not valid JSON: \(error!)")
                    return nil
                }
            } else {
                print("Could not load level file: \(filename), error: \(error!)")
                return nil
            }
        } else {
            print("Could not find level file: \(filename)")
            return nil
        }
    }
}

最佳答案

您使用的不是最新的 swift 代码,

我更正了你的代码,注意你应该选择最适合你的NSDataReadingOption,我选择NSDataReadingOptions.DataReadingMapped

加上一些api不把错误作为输入,所以我删除了它。

请注意,当您发现错误时,您可以使用明确声明了它的对象 error,这就是我删除错误变量的原因

最后,解析json的新api,应该在try catch里,所以我加了try catch

import Foundation

extension Dictionary {
    static func loadJSONFromBundle(filename: String) -> Dictionary<String, AnyObject>? {
        if let path = NSBundle.mainBundle().pathForResource(filename, ofType: "json") {


            do{
                let data = try NSData(contentsOfFile: path, options: NSDataReadingOptions.DataReadingMapped)
                do{
                    let dictionary: AnyObject? = try NSJSONSerialization.JSONObjectWithData(data,
                        options: NSJSONReadingOptions())
                    if let dictionary = dictionary as? Dictionary<String, AnyObject> {
                        return dictionary
                    } else {
                        print("Level file '\(filename)' is not valid JSON")
                        return nil
                    }
                }catch {
                    print("Level file '\(filename)' is not valid JSON: \(error)")
                    return nil
                }


            }catch {
                print("Could not load level file: \(filename), error: \(error)")
                return nil
            }

        } else {
            print("Could not find level file: \(filename)")
            return nil
        }
    }
}

所以基本上:

  1. NSData(contentsOfFile: path, options: 不再出错。
  2. 字典:AnyObject? = 试试 NSJSONSerialization.JSONObjectWithData(数据, options 不再出错。
  3. 从文件中获取 NSData 需要 try catch
  4. 从 NSData 获取 json 对象需要 try catch

关于ios - 尝试以 swift 2 重载错误的形式读取 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33412757/

相关文章:

ios - 在Objective-C中以相同的动画显示和隐藏UIView?

iPhone Facebook 请求错误

java - 使用 Gson 反序列化 Map<String, List<?>>

javascript - 将 JSON 对象映射到单独的 div 中

php - 如何通过PHP从MySQL db获取数据并输出为JSON,然后在JQuery中跨域ajax请求数据

iphone - 无法加载从设备上运行的包中的 nib 引用的图像

xcode - 从 mac AppStore 安装 Xcode 时,DMG 在哪里?

ios - 嵌入式 dylibs/frameworks 仅支持 iOS 8.0 及更高版本的架构 armv7

ios - swift2解密MD5

javascript - 将地理位置从 Swift 2 ViewController 传递到 Javascript 方法