【问题标题】:Why isEmpty not working here ?为什么 isEmpty 不在这里工作?
【发布时间】:2017-11-16 19:57:42
【问题描述】:

我在这里做了一个结构 struct dicStruct<T,U>{}

这里是我的实例

var instanceOfDicStruct:dicStruct<Int,String> = dicStruct()

isEmpty 在这里不起作用,任何人都可以解释一下给我一些参考吗?

instanceOfDicStruct.isEmpty

【问题讨论】:

  • Would = nil work
  • 是的 Nil 工作但我需要知道为什么 isEmpty 不只是为了兴趣而工作。
  • 您定义了自己的类型dicStruct,但没有isEmpty 方法。不确定您的预期。
  • 请注意,这是 Swift 约定以大写字母开头的结构命名。您应该花点时间阅读 Apple Swift Book itunes.apple.com/us/book/the-swift-programming-language-swift-4/…。你可能想要达到的是struct MyStruct { var instancePropertyOfMyStruct: [Int: String] = [:] } var instanceOfMyStruct = MyStruct() instanceOfMyStruct.instancePropertyOfMyStruct.isEmpty // true
  • 哇,我也是 Rush 的忠实粉丝,我也是音乐家 xD 顺便说一句,我使用 swift 约定仍然无法正常工作..我知道我在 swift 中做什么是新的,我只是想学习swift type Struct ..谢谢

标签: swift


【解决方案1】:

具有两个通用占位符类型≠字典的结构。

如果你需要一个字典,然后像这样创建一个字典的实例:

var dictionaryInstance = [Int: String]()

isEmpty 现在可以使用:

print(dictionaryInstance.isEmpty) // true

你也可以创建一个类型别名:

typealias MyDictionary = [Int: String]

var myDictionaryInstance = MyDictionary()

【讨论】:

  • 谢谢 ..我只是想学习 swift Dictionary 和其他类型结构 ..如果你知道任何关于它的博客或书籍 ..它将帮助我
  • @EkramulHoque 花点时间阅读 Apple 的 Swift Book。那里有很多关于所有 Swift 类型的操作系统示例,包括字典,它是免费的itunes.apple.com/br/book/the-swift-programming-language-swift-4/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-24
  • 1970-01-01
  • 2011-07-20
相关资源
最近更新 更多