0

I've looked around but I don't find an answer to fix this error that has been bugging me. I tried adding a "as! NSMutableArray" but that gave me another error. Any ideas on how to fix it? I converted my project from Objective-C to Swift, so hopefully the code is all good I had 20+ errors now I'm down to 3 errors. Thank you.

Error Message:

'jsonObject' produces 'Any', not the expected contextual result type 'NSMutableArray'

code error picture

Code for retrieving data from server

// Retrieving Data from Server
func retrieveData() {

    let getDataURL = "http://ip/example.org/json.php"
    let url: NSURL = NSURL(string: getDataURL)!

    do {

        let data: NSData = try NSData(contentsOf: url as URL)
        jsonArray = JSONSerialization.jsonObject(with: data, options: nil)
    }
    catch {
        print("Error: (data: contentsOf: url)")
    }

    // Setting up dataArray
    var dataArray: NSMutableArray = []

    // Looping through jsonArray
    for i in 0..<jsonArray.count {

        // Create Data Object

        let dID: String = (jsonArray[i] as AnyObject).object(forKey: "id") as! String
        let dName: String = (jsonArray[i] as AnyObject).object(forKey: "dataName") as! String
        let dStatus1: String = (jsonArray[i] as AnyObject).object(forKey: "dataStatus1") as! String
        let dStatus2: String = (jsonArray[i] as AnyObject).object(forKey: "dataStatus2") as! String
        let dURL: String = (jsonArray[i] as AnyObject).object(forKey: "dataURL") as! String

        // Add Data Objects to Data Array
        dataArray.add(Data(dataName: dName, andDataStatus1: dStatus1, andDataStatus2: dStatus2, andDataURL: dURL, andDataID: dID))
    }

    self.myTableView.reloadData()
}
WokerHead
  • 947
  • 2
  • 15
  • 46

1 Answers1

3

The jsonObject function will return a value of type Any but the jsonArray's type of NSMutableArray. And this function will throw an error if something is wrong, put a try keyword before it. In my experience, let change the type of jsonArray to array of dictionary, so you will extract data with ease.

    do {
        let data: Data = try Data(contentsOf: url as URL)
        let jsonArray: [[String: AnyObject]] = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! [[String: AnyObject]]
        print("json: \(jsonArray)")

        for dict in jsonArray {
            let dataName = dict["dataName"] as! String
            print("dataName: \(dataName)")
        }
    }
    catch {
        print("Error: (data: contentsOf: url)")
    }
Danh Huynh
  • 2,337
  • 1
  • 15
  • 18
  • Okay so I tried the code and I got 2 errors, one saying "Cannot invoke initializer for type 'Data' with an argument list of type '(contentsOf: URL)' " and the second error saying "Ambiguous reference to member 'jsonObject(with:options:)' " - No idea what they mean – WokerHead Nov 07 '16 at 03:10
  • Did you use Swift 2 or 3? – Danh Huynh Nov 07 '16 at 03:18
  • I used Swift 3. – WokerHead Nov 07 '16 at 03:20
  • I see you have own type named `Data`, let use other name. In Swift 3, Apple removed prefix `NS`, so `NSData` became `Data`. – Danh Huynh Nov 07 '16 at 03:23
  • Let rename this type `Data(dataName: dName, andDataStatus1: dStatus1, andDataStatus2: dStatus2, andDataURL: dURL, andDataID: dID)` – Danh Huynh Nov 07 '16 at 03:26
  • Okay so anything with the word "Data" I rename even in my NSObject class since it gets it from there? – WokerHead Nov 07 '16 at 03:27
  • Okay awesome everything worked but just to understand the code you gave me, you have this line in it "let dataName = dict["dataName"] as! String" is this replacing the objects that I made below? – WokerHead Nov 07 '16 at 03:43
  • Also, since I renamed everything to "game" do I change anything in your code? For example this code " let data: Data = try Data(contentsOf: url as URL) " – WokerHead Nov 07 '16 at 03:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/127511/discussion-between-brosimple-and-danh-huynh). – WokerHead Nov 07 '16 at 04:44
  • I ran into a problem, nothing is showing up in the table view – WokerHead Nov 07 '16 at 04:47