Syed Munim Raza Syed Munim Raza - 3 months ago 9
Swift Question

Issue checking count at index 0 in swift while Objective-C as bridge

Sorry if this is really simple question but I am having difficulty in finding out count of NSArray following is technique I follow that goes in crashing the code.

Swift:

var employeeInfoArray: NSArray = []

var savedValue: String?
savedValue = defaults.stringForKey("FromToDuration")
employeeInfoArray = [EmployeeDatabase .sharedSingletonDatabase().evaluationList(savedValue)]

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return (employeeInfoArray[0].count)

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
var simpleId: String?
simpleId = "EmpTableViewCell"

let cell:EmpTableViewCell = tableView.dequeueReusableCellWithIdentifier(simpleId!) as! EmpTableViewCell
cell.empNameLabel.text = employeeInfoArray[0][indexPath.row].name
cell.empIDLabel.text = String(employeeInfoArray[0][indexPath.row].uniqueId)

return cell
}


My employeeInfoArray has data in following hierarchy coming from a bridge from Objective C model:

NSArray
->NSArray
-->Employee1 (MyCustom Class Object)
-->Employee2 (MyCustom Class Object)
-->Employee3 (MyCustom Class Object)


On Request for detail Hierarchy

Issue:

When I return 3 in numberOfRowsInSection I am able to display all 3 employees details on tableView.

When I return (employeeInfoArray.count) I get only 1 Employee details Viewed

When I return return (employeeInfoArray[0].count). It Crashes.

Now how can I get count of 0 index ?

Please don't mark it as duplicate I tried searching this question from last 4 hours.

Answer

You are unnecessarily putting your employees inside another array by using the [] around the outside of your array access.

You should also make your code more robust in the event that the defaults value is missing. Also, in Swift there is no need to declare the type of a variable if Swift can infer it from the value that is assigned.

Finally, minimise the use of NSArray; get the data into a Swift array as soon as you can, since then the contents are typed.

var employeeInfoArray = [Employee]()

override func viewDidLoad() {
    super.viewDidLoad()
    if let savedValue = defaults.stringForKey("FromToDuration") {
         employeeInfoArray = EmployeeDatabase.sharedSingletonDatabase().evaluationList(savedValue) as! [Employee]
    }
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return employeeInfoArray.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    var simpleId = "EmpTableViewCell"

    let cell = tableView.dequeueReusableCellWithIdentifier(simpleId, foIndexPath: indexPath) as! EmpTableViewCell
    let employee = employeeInfoArray[indexPath.row]
    cell.empNameLabel.text = employee.name
    cell.empIDLabel.text = "\(employee.uniqueId)"

    return cell
}
Comments