【问题标题】:Swift: [__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]'Swift:[__NSPlaceholderDictionary initWithObjects:forKeys:count:]:尝试从对象 [1] 中插入 nil 对象
【发布时间】:2014-06-13 16:55:08
【问题描述】:

我有一个 UITextView,我想将此文本存储在 NSDictionary 中。

我有这段代码:

 var imageData = UIImageJPEGRepresentation(photoInfo["UIImagePickerControllerOriginalImage"] as UIImage, 100.0)

        var description = photoDesc.text

        println(description)

        var parameters: NSDictionary = ["user":"admin", "desc" : description, "image" : imageData]

        println(parameters)

println() 正确输出“描述”,但我的代码在这一行崩溃: var 参数:NSDictionary = ["user":"admin", "desc" : description, "image" : imageData]

出现此错误:

[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]'

如果我从 NSDictionary 中删除“desc”键,我不会收到任何错误。

有什么想法吗?

已解决:

var description: String = photoDesc.text

【问题讨论】:

  • 你检查过 description 是否为 nil 吗?
  • 为什么不将它存储在 Swift 字典中?即 Dictionary
  • 我的意思是字典。不能再编辑我的评论了。
  • 谢谢米莉,我已经解决了:var description: String = photoDesc.text

标签: swift


【解决方案1】:

您不能将nil 存储在NSDictionary 中。将NSNull 传递给您的description

【讨论】:

  • Unexpected interface name 'NSNull': expected expression
  • @AndrewKoster NSNull()
  • NSDictionary* eventData = @{ @"selectedItem": NSNull(), };
  • @AndrewKoster 你使用的是swift还是objective c?它适用于https://online.swiftplayground.run/。去那里试试。 var parameters: NSDictionary = ["user":NSNull()]。使用您的代码,我会得到 string literals in Swift are not preceded by an '@' sign 之类的东西
  • @AndrewKoster 如果您使用的是目标 c,请删除您的反对票。这个问题是关于 swift 的,并且只标记了 swift。
猜你喜欢
  • 1970-01-01
  • 2014-07-16
  • 2020-11-08
  • 2016-05-23
  • 2023-03-29
  • 2014-08-27
  • 2017-12-16
  • 1970-01-01
  • 2012-08-09
相关资源
最近更新 更多