在Swift中展开可选项时发现nil

时间:2015-08-22 07:23:15

标签: swift optional unwrap

我知道这可能是一个简单的,只是不确定我在这里缺少什么需要一双新鲜的眼睛。我在这里理解我需要解开我正在尝试做的但是它一直在失败,我做错了什么可以帮助吗?下面是代码(我可能会踢自己)

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: 
NSIndexPath) -> UITableViewCell {

    var currentLocation = self.locations[indexPath.row]
    var displayLocation = currentLocation["User"] as! PFObject

    let cell = UITableViewCell()

    if let display = displayLocation as PFObject? {

    cell.textLabel?.text = display["currentLocation"] as? String
    }

    return cell

}
对不起,伙计们忘了提到它在这条线上失败了

var displayLocation = currentLocation["User"] as! PFObject

3 个答案:

答案 0 :(得分:1)

错误表示您使用currentLocation["User"]PFObject投射到as!这是不可能的。因为currentLocation["User"]可能是nil

如果是nil,则无法将其转换为PFObject

答案 1 :(得分:0)

SELECT DISTINCT column_name FROM table_name; 字典缺少“User”键的值,或者值为nil。

答案 2 :(得分:0)

保持简单,就像这样

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table">
    <thead>
        <tr class="tH">
            <th>HEAD A</th>
            <th>HEAD B</th>
        </tr>
    </thead>
    <tbody>
        <!-- 1 -->
        <tr class="tB header1">
            <td class=" rowClic1">Clic here to close/open first Row</td>
            <td></td>
        </tr>
    </tbody>
    <tbody class="bdBody">
        <!-- 1.1 -->
        <tr class="tD header2">
            <td class="rowClic2">Clic here to open children Row </td>
            <td></td>
        </tr>
        <tr class="tD">
            <td></td>
            <td>data</td>
        </tr>
         <tr class="tC">
            <td></td>
            <td>data</td>
        </tr>
         <tr class="tC">
            <td></td>
            <td>data</td>
        </tr>
        <!-- 1.2 -->
        <tr class="tC header2">
            <td class="rowClic2"> Clic here to open children Row</td>
            <td></td>
        </tr>
        <tr class="tC">
            <td></td>
            <td>data</td>
        </tr>
        <!-- 1.3 -->
        <tr class="tD header2">
            <td class="rowClic2">Clic here to open children Row</td>
            <td></td>
        </tr>
         <tr class="tD">
            <td></td>
            <td>data</td>
        </tr>
         <tr class="tC">
            <td></td>
            <td>data</td>
        </tr>
    </tbody>

     <tbody>
        <!-- 2 -->
        <tr class="tB header1">
            <td class=" rowClic1">Clic here to open first Row </td>
            <td></td>
        </tr>
    </tbody>
    <tbody class="bdBody">
        <!-- 2.1 -->
        <tr class="tD header2">
            <td class="rowClic2">Clic here to open children Row</td>
            <td></td>
        </tr>
        <tr class="tD">
            <td></td>
            <td>data</td>
        </tr>
        <!-- 2.2 -->
        <tr class="tC header2">
            <td class="rowClic2">Clic here to open children Row</td>
            <td></td>
        </tr>
        <tr class="tC">
            <td></td>
            <td>data</td>
        </tr>
         <tr class="tC">
            <td></td>
            <td>data</td>
        </tr>
         <tr class="tC">
            <td></td>
            <td>data</td>
        </tr>
        <!-- 2.3 -->
        <tr class="tD header2">
            <td class="rowClic2">Clic here to open children Row</td>
            <td></td>
        </tr>
         <tr class="tD">
            <td></td>
            <td>data</td>
        </tr>
         <tr class="tC">
            <td></td>
            <td>data</td>
        </tr>
         <tr class="tC">
            <td></td>
            <td>data</td>
        </tr>
         <tr class="tC">
            <td></td>
            <td>data</td>
        </tr>
    </tbody>
</table>