5
\$\begingroup\$

So I recently applied to this job for iOS Developer and was invited to complete the following code challenge:

As a next step, please complete this coding exercise in order to proceed with our interview process. If you do well & pass, you will be connected to a Software Engineer to discuss your exercise for an hour zoom video call. Take as much time as needed.

Problem statement:

Please write an (Android/iOS) app that retrieves the data from https://fetch-hiring.s3.amazonaws.com/hiring.json. This will return a json array of items. Using this list of items, display all the items grouped by "listId" to the UI. Sort the results first by "listId" then by "name" when displaying. Filter out any items where "name" is blank or null. The final result should be displayed to the user in an easy-to-read list."

My solution is here.

View Controller is as follows:

import UIKit



class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return tableData[section].count
}

@IBOutlet weak var dataTableView: UITableView!
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "DataViewCell", for: indexPath) as? DataCell
    cell?.textSpace.text=tableData[indexPath.section][tableIndices[indexPath.section][indexPath.row]]
    return cell!
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return "List ID: "+String(section+1)
}

func numberOfSections(in tableView: UITableView) -> Int {
    return tableData.count
}



var tableData: [[Int:String]]=[[:]]
var tableIndices: [[Int]]=[[]]

override func viewDidLoad() {
    super.viewDidLoad()
    
    
    URLSession.shared.dataTask(with: URL(string: "https://fetch-hiring.s3.amazonaws.com/hiring.json")!)
                 {
                     data,response,error  in
                     
                    
                    let json=try? JSONSerialization.jsonObject(with: data!, options: []) as! [[String:Any]]
                    
                    for items in json!
                    {
                        if(type(of: items["name"]!) != NSNull.self && items["name"]! as! String != "")
                        {
                            let listID=Int(String(describing: items["listId"]!))!
                          
                            while(self.tableData.count < listID)
                            {
                                self.tableData.append([:])
                                self.tableIndices.append([])
                            }
                            
                            self.tableIndices[listID-1].append(Int(String(describing: items["id"]!))!)
                            self.tableData[listID-1][Int(String(describing: items["id"]!))!       ]=items["name"]! as! String
                            self.tableIndices[listID-1].sort()
                            
                        }
                    }
                 
                     DispatchQueue.main.async {
                        self.dataTableView.delegate=self
                        self.dataTableView.dataSource=self
                        self.dataTableView.reloadData()
                         
                     }
            
                 }.resume()
                  

}


}

Two days after submitting, I got the following feedback:

The exercise featured a lot of Swift code that forced unwrapping instead of using nil safe paradigms such as let or guard, and the items displayed in the list would have benefited from a struct or class defining the object to better separate the business logic from the view controller.

Are they being too nit picky here, or is my code really that screwed up?

\$\endgroup\$
0
3
\$\begingroup\$

They're not being too nit-picky. DataTask's completion handler will return nil for data when it couldn't get the data, like when the network is offline. Force unwrapping 'data' means you deliberately crash the application when that happens. That would be bad.

"separate the business logic from the view controller": They expect you to make a data type to parse the JSON into, perhaps like this:

struct Item: Decodable {
    let id: Int
    let listId: Int
    let name: String?
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.