删除Optional和nil

时间:2018-04-27 20:09:54

标签: json swift

cellForRow

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) 
    -> UITableViewCell {
        guard let cell = 
            tableView.dequeueReusableCell(withIdentifier: "EventsCell") as? EventsCell 
            else { return UITableViewCell() }
        cell.homeLabel.text = events[indexPath.row].homeTeamName
        cell.awayLabel.text = events[indexPath.row].awayTeamName
        cell.homeGoalLbl.text = 
            String (describing: events[indexPath.row].result.goalsHomeTeam)
        cell.awayGoalLbl.text = 
            String (describing: events[indexPath.row].result.goalsAwayTeam)
        return cell
    }

Codables

class EventsFull: Codable {
    let fixtures: [EventsData]
    init(fixtures: [EventsData]) {
        self.fixtures = fixtures
    }
}

class ResultsData: Codable {
    let goalsHomeTeam: Int?
    let goalsAwayTeam: Int?
    init(goalsHomeTeam: Int,goalsAwayTeam: Int) {
        self.goalsHomeTeam = goalsHomeTeam
        self.goalsAwayTeam = goalsAwayTeam
    }
}

class EventsData: Codable {
    let date: String
    let status: String
    let matchday: Int
    let homeTeamName: String
    let awayTeamName: String
    let result: ResultsData
    let odds: Double?

    init(date: String, status: String, matchday: Int, homeTeamName: String, awayTeamName: String, result: ResultsData, odds: Double) {
        self.date = date
        self.status = status
        self.matchday = matchday
        self.homeTeamName = homeTeamName
        self.awayTeamName = awayTeamName
        self.result = result
        self.odds = odds  
    }
}

enter image description here

控制台:

downloaded

Optional(3)
Optional(2)

如何从视图中删除Optional以及如何" nil"没出现?

1 个答案:

答案 0 :(得分:3)

如果我们获得assertTrue(Arrays.deepEquals(arr1, arr2)); ,那么哪些字符串应该出现在目标标签中?你需要指定它。然后你可以这样写:

nil

这将同时完成两项工作 - 它同时消除了"可选"和" nil"作为可能的标签值。

[见guide