【问题标题】:NSString variable to UITextField's text on SwiftNSString 变量到 Swift 上的 UITextField 的文本
【发布时间】:2014-08-20 08:15:52
【问题描述】:

我需要从 NSMutableDictionary 中获取一个 NSString 并将其分配给 UITextField 的文本。

但我遇到了一个例外;解包 Optional 值时意外发现 nil

当我记录它写入控制台的字符串时。

我的代码如下;

    let tempString:NSString! = tempDict.objectForKey("name") as? NSString

    NSLog("%@",tempString)

    cell.textLabel.text = tempString  //This line gives the exception

【问题讨论】:

  • 好吧,如果字符串没问题,那么它可能是单元格或 textLabel。你确定那些不是零?您是否忘记在 Interface builder 中连接其中一个?
  • 显示您的单元格声明
  • var cell:UITableViewCell! = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell if (cell == nil) { cell = UITableViewCell(style: UITableViewCellStyle.Subtitle,reuseIdentifier:cellIdentifier) }

标签: ios nsstring swift uitextfield


【解决方案1】:

您可以通过添加 if 语句安全地解开 cell.textLabel 来阻止此操作。

if var label = cell.textLabel{
    label.text = "This is a title"
}

希望这会有所帮助.. :)

【讨论】:

    【解决方案2】:

    你可以试试这个。它有助于摆脱我遇到的同样问题

    让 userName : NSString = result.valueForKey("name") as! NSString self.lblUserName.text = 用户名作为字符串

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多