【问题标题】:swift ios: different errors for similar data .. very strangeswift ios:类似数据的不同错误..非常奇怪
【发布时间】:2014-08-30 09:49:11
【问题描述】:

发生了一些奇怪的事情。

我的 TableDataArray 中有这个:

(
    {
        count = 0;
        title = Open;
    },
        {
        count = 20;
        title = Closed;
    },
        {
        count = 0;
        title = Pending;
    },
        {
        count = 10;
        title = Queue;
    }
)

当我这样做时:

var rowData: NSDictionary = TableDataArray[indexPath.row] as NSDictionary

var maintext: String?  = rowData["title"] as NSString
println(maintext)

if (maintext  != nil ){
    cell.textLabel.text = maintext
}

它有效,我在表格中看到了标题。

但只要我添加这些行:

var detailtext: String?  = rowData["count"]  as NSString ## tried also as Int, NSInteger, similar fate

println(detailtext)

if (detailtext != nil)  {
    cell.detailTextLabel.text = detailtext
}

应用程序因“Swift dynamic cast failed”而崩溃,我无法弄清楚原因。

另一个是如果我进行另一个 API 调用,结果是相似的,但不是崩溃,它只是显示......文本和详细文本。

但在另一个 api 调用中,它崩溃了,但出现“致命错误:在展开可选值时意外发现 nil”......而在另一个调用中,它只是说 String 不可转换到 Uint8...

这让我很烦。相同的 API 调用,相似的结果,但它可以合二为一,并且崩溃并导致不同的结果...

所以问题是,我如何检测和解决此类问题,然后显示 detailText... 因为值在那里。

谢谢。

【问题讨论】:

  • rowData["count"] 是否持有 NSNumber?
  • 如何声明 TableDataArray。

标签: ios uitableview swift


【解决方案1】:

您的值不能是IntString,因为NSDictionarys 中的值必须是对象。你的count 是一个NSNumber,它是一个基本数字类型的对象包装器。

要安全地从您的NSDictionary 中提取号码,请使用以下样式:

if let count = rowData["count"] as? NSNumber {
    // If I get here, I know count is an NSNumber.  If it were some other type
    // it wouldn't crash, but I wouldn't get to this point.

    cell.detailTextLabel.text = "\(count)"
}

这可以保护您免受各种问题的困扰。当您从NSDictionary 请求一个项目时,字典中可能不存在该键,在这种情况下,结果将为nil。如果您尝试将其直接转换为预期类型,您将收到 fatal error: unexpectedly found nil while unwrapping an Optional value 消息。使用上面的样式,nil 被优雅地处理,没有错误结果,你只是不要进入块。

您的count 似乎可以有多种类型。您可以使用switch 以更简洁的方式处理此问题:

switch rowData["count"] {
case let count as NSNumber:
    cell.detailTextLabel.text = "\(count)"
case let count as NSString:
    cell.detailTextLabel.text = count
case nil:
    println("value not in dictionary")
default:
    println("I still haven't identified the type")
}

【讨论】:

  • 嗨 vacawama,谢谢你的回答,我知道我对 NSDictionary 的理解是错误的。我将其视为纯文本多维数组。现在我的应用程序没有崩溃,但现在它只列出了 Zeros 而不是 20 和 10 值。
【解决方案2】:

这行得通:

    if let maintext = rowData["title"] as? NSString {
        println(maintext)
        cell.textLabel.text = maintext

    }

    if var count = rowData["count"] as? NSNumber {
        // If I get here, I know count is an NSNumber.  If it were some other type
        // it wouldn't crash, but I wouldn't get to this point.
        println("\(count)")
        cell.detailTextLabel.text = "\(count)"


    }

    if var count = rowData["count"] as? String {
        // If I get here, I know count is an NSNumber.  If it were some other type
        // it wouldn't crash, but I wouldn't get to this point.
        println(count)
        cell.detailTextLabel.text = count


    }

但这是正确的方法吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-16
    相关资源
    最近更新 更多