Swift unwrapped值仍显示为Optional

时间:2017-03-18 04:54:41

标签: ios swift optional forced-unwrapping

我很难在没有Optional(...)的情况下显示名称值。我以为我在打开它们,但是我无法摆脱它们。我在StackOverflow上找到了一个解决方案,但在那种情况下,涉及as?,这不是这里的情况。违规代码如下。有什么方法可以解开这些吗?我假设我错过了一些明显的东西。感谢。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! ClientCell

    cell.setBackground()

    let clientKey = clientSectionTitles[(indexPath as NSIndexPath).section]
    if let clientValues = clientDict[clientKey] {
        if self.sortBy == "First" {
            cell.clientName.text = clientValues[(indexPath as NSIndexPath).row].firstName! + " " + clientValues[(indexPath as NSIndexPath).row].lastName!
        } else {
            cell.clientName.text = clientValues[(indexPath as NSIndexPath).row].lastName! + ", " + clientValues[(indexPath as NSIndexPath).row].firstName!
        }
    }

    return cell
}

*编辑: 以下是构建字典的代码:

func createClientDict() {
    var clientName: String?
    clientDict          = [String: [Client]]()
    clientSectionTitles = [String]()

    if self.sortBy == "First" {
        apiResults.sort (by: { $0.firstName < $1.firstName })
    } else {
        apiResults.sort (by: { $0.lastName < $1.lastName })
    }

    for c in apiResults {
        if self.sortBy == "First" {
            clientName = "\(c.firstName!) \(c.lastName!)"
        } else {
            clientName = "\(c.lastName!), \(c.firstName!)"
        }

        // Get the first letter of the name and build the dictionary
        let clientKey = clientName!.substring(to: clientName!.characters.index(clientName!.startIndex, offsetBy: 1))
        if var clientValues = clientDict[clientKey] {
            clientValues.append(c)
            clientDict[clientKey] = clientValues
        } else {
            clientDict[clientKey] = [c]
        }
    }

    // Get the section titles from the dictionary's keys and sort them in ascending order
    clientSectionTitles = [String](clientDict.keys)
    clientSectionTitles = clientSectionTitles.sorted { $0 < $1 }
}

*编辑:我还应该提到这只发生在Swift 3中。它在Swift 2.2中运行良好。

2 个答案:

答案 0 :(得分:1)

以下内容来自Swift编程语言集合类型文档:

  

您还可以使用下标语法从中检索值   特定键的字典。因为可以请求一个   没有值的键,字典的下标返回一个   字典值类型的可选值。如果是字典   包含所请求键的值,下标返回一个   包含该键的现有值的可选值。除此以外,   下标返回nil:

所以clientValues[(indexPath as NSIndexPath).row]会返回一个可选值

尝试

cell.clientName.text = (clientValues[(indexPath as NSIndexPath).row].firstName!)!

*已编辑 - 实际上更好地使用可选绑定:

if let fname = clientValues[(indexPath as NSIndexPath).row].firstName! {
   cell.clientName.text = fname }

答案 1 :(得分:0)

我认为该问题与clientKey变量有关,该变量具有可选值。尝试在使用之前解开它,如下所示:

if let clientKey = clientSectionTitles[(indexPath as NSIndexPath).section] {
    if let clientValues = clientDict[clientKey] {
        if self.sortBy == "First" {
            cell.clientName.text = clientValues[(indexPath as NSIndexPath).row].firstName! + " " + clientValues[(indexPath as NSIndexPath).row].lastName!
        } else {
            cell.clientName.text = clientValues[(indexPath as NSIndexPath).row].lastName! + ", " + clientValues[(indexPath as NSIndexPath).row].firstName!
        }
    }
}

===编辑:===

 let clientKey = clientSectionTitles[(indexPath as NSIndexPath).section] 
        if let clientValues = clientDict[clientKey] {
            if self.sortBy == "First" {
                cell.clientName.text = clientValues[(indexPath as NSIndexPath).row].firstName! + " " + clientValues[(indexPath as NSIndexPath).row].lastName!
            } else {
                cell.clientName.text = clientValues[(indexPath as NSIndexPath).row].lastName! + ", " + clientValues[(indexPath as NSIndexPath).row].firstName!
            }
        }