BroSimple BroSimple - 1 month ago 8
JSON Question

Swift errors when handling data from server, json

I converted my project from Objective-C to Swift but I ran into some errors.

First set of errors:

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let CellIdentifier = "Cell"
var cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier)!

if cell != cell {
cell = CustomCell(style: UITableViewCellStyle.default, reuseIdentifier: CellIdentifier)
}

// Configuring the cell
Data * dataObject

if !isFiltered {
if indexPath.section == 0 {
dataObject = followedArray[indexPath.row]
cell.populateCell(dataObject, isFollowed: true, indexPath: indexPath, parentView: self)
}
else if indexPath.section == 1 {
dataObject = dataArray[indexPath.row]
cell.populateCell(dataObject, isFollowed: false, indexPath: indexPath, parentView: self)
}
}
else {
dataObject = filteredArray[indexPath.row]
cell.populateCell(dataObject, isFollowed: false, indexPath: indexPath, parentView: self)
}
return cell
}


Here is a picture of the errors:

enter image description here

Here is the Data NSObject Class

class Data: NSObject {

//Strings
var dataName: String?
var dataID: String?
var dataStatus1: String?
var dataStatus2: String?
var dataURL: String?

override init() {

}

//Converting Strings into Objects

init(dataName dName: String,
andDataStatus1 dStatus1: String,
andDataStatus2 dStatus2: String,
andDataURL dURL: String,
andDataID dID: String)
{

super.init()

self.dataName = dName
self.dataStatus1 = dStatus1
self.dataStatus2 = dStatus2
self.dataURL = dURL
self.dataID = dID

}

}


Thats the first set of errors.

Now the Second Set of errors

This code is for retrieving the data from the server

// Retrieving Data from Server
func retrieveData() {

let url: NSURL = NSURL(string: getDataURL)!
let data: NSData = NSData(contentsOfURL: url)
jsonArray = NSJSONSerialization.JSONObjectWithData(data, options: kNilOptions, error: nil)

// Setting up data array
dataArray = NSMutableArray()

// Looping through jsonArray
for i in 0..<jsonArray.count {
// Create Data Object
let dID: String = jsonArray[i]["id"] as! String!
let dName: String = jsonArray[i]["dataName"]
let dStatus1: String = jsonArray[i]["dataStatus1"]
let dStatus2: String = jsonArray[i]["dataStatus2"]
let dURL: String = jsonArray[i]["dataURL"]

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

self.myTableView.reloadData()
}


Here is a picture of the errors:

enter image description here

enter image description here

I tried renaming some of the code from the errors but that just ended with more errors so I left it as originally converted. I'm learning Swift but these are deeper side of coding but in fixing it, I will be learning a lot more. Thank you for any help!

New Fixes/ Errors

enter image description here

enter image description here

Answer

You did not convert to Swift very well at all. And the error log basically explains everything that is wrong very well.

Line 109

var dataObject: Data

Line 227

jsonArray = JSONSerialization.JSONObjectWithData(data, options: nil, error: nil)

Line 230

var dataArray = []

Line 235+

let dID: String = jsonArray[i].objectForKey("id") as! String!
...
let dURL: NSURL = jsonArray[i].objectForKey("dataURL") as! NSURL

Line 242

dataArray.append(Data(dataname: dName, andDataStatus1: dStatus1, andDataStatus2: dStatus2, andDataURL: dURL, andDataID: dID))