Problem Getting an NSData Request to Work in Swift 2.0

I hope someone can help me deal with snafu, which I have with the application I'm trying to write (or learn to write) in Swift 2.0. This previously worked in Swift 1.2, but after the necessary transformations, I constantly encounter an error;

Cannot call initializer of type "NSData" with argument list type (contenOfURL: NSURL, options: NSDataReadingOptions, error: no)

Here is my slightly truncated code that I use;

... class func fetchMinionData() -> [Minion] { let myURL = "https://myurl/test.json" let dataURL = NSURL(string: myURL) let data = NSData(contentsOfURL: dataURL!, options: NSDataReadingOptions.DataReadingMappedIfSafe, error: nil) //THIS IS THE LINE THAT THROWS THE ERROR let minionJSON = JSON(data) var minions = [Minion]() for (_ , minionDictionary) in minionJSON { minions.append(Minion(minionDetails: minionDictionary)) } return minions } ... 

Please note that I plan to use SwiftyJSON to further analyze the data after loading it. I search endlessly online, but I just can't figure it out! Thanks!

+4
source share
2 answers

If you are working with Swift 2, you should not pass the last argument to "error". Instead, try initializing NSData. If accessing data requires an external result, the initialization result is in var and convert to allow the modified code

 var optData:NSData? = nil do { optData = try NSData(contentsOfURL: dataURL!, options: NSDataReadingOptions.DataReadingMappedIfSafe) } catch { print("Handle \(error) here") } if let data = optData { // Convert data to JSON here } 
+10
source

Sample dictionary code :) Swift 2.0 https://github.com/DaRkD0G/LoadExtension/edit/master/LoadExtensionDictionary.swift

 enum EHError: ErrorType { case Nil(String) case NSData(String) case JSON(String) } extension Dictionary { /** Loads a JSON file from the app bundle into a new dictionary - parameter filename: File name - throws: PathForResource / NSData / JSON - returns: Dictionary<String, AnyObject> */ static func loadJSONFromBundle(filename: String) throws -> Dictionary<String, AnyObject> { guard let path = NSBundle.mainBundle().pathForResource(filename, ofType: "json") else { throw EHError.Nil("[EasyHelper][loadJSONFromBundle][->pathForResource] The file could not be located\nFile : '\(filename).json'") } guard let data = try? NSData(contentsOfFile: path, options:NSDataReadingOptions()) else { throw EHError.NSData("[EasyHelper][loadJSONFromBundle][->NSData] The absolute path of the file not find\nFile : '\(filename)'") } guard let jsonDict = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions()) as? Dictionary<String, AnyObject> else { throw EHError.JSON("[EasyHelper][loadJSONFromBundle][->NSJSONSerialization]Error.InvalidJSON Level file '\(filename)' is not valid JSON") } return jsonDict } } 

If I'm not mistaken, you think that

 /** Loads a JSON file from the app bundle into a new dictionary - parameter filename: File name - throws: EHError : PathForResource / NSData / JSON - returns: [String : AnyObject] */ static func loadJSONFromBundle(filename: String, nameJson:String) throws -> [String : AnyObject] { guard let path = NSBundle.mainBundle().pathForResource(filename, ofType: "json") else { throw EHError.Nil("[EasyHelper][loadJSONFromBundle][->pathForResource] The file could not be located\nFile : '\(filename).json'") } guard let data = try? NSData(contentsOfFile: path, options:NSDataReadingOptions()) else { throw EHError.NSData("[EasyHelper][loadJSONFromBundle][->NSData] The absolute path of the file not find\nFile : '\(filename)'") } guard let jsonDict = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) as? [String : AnyObject] else { throw EHError.JSON("[EasyHelper][loadJSONFromBundle][->NSJSONSerialization] Invalid JSON\n nameJson '\(nameJson)'\nFile '\(filename)'") } return jsonDict } 
0
source

All Articles