简体   繁体   English

双方括号在swift中意味着什么?

[英]what does double square brackets mean in swift?

the below is the sample code in swift. 以下是swift中的示例代码。

var loadedMessages = [[Message]]()

Message is a custom class. 消息是一个自定义类。 i'm not sure what [[Message]] () is doing. 我不确定[[Message]]()正在做什么。

It is specifying that your variable loadedMessages is an array of arrays that contains Message objects. 它指定您的变量loadedMessages是包含Message对象的数组数组。 A JSON representation of loadedMessages might look like: loadedMessages的JSON表示可能如下所示:

loadedMessages: [
  [ <Message>, <Message>, <Message> ],
  [ <Message>, <Message>, <Message> ]
]

A quick Playground implementation of something similar can give you a pretty good introspection of the situation: 快速的Playground实现类似的东西可以让你对这种情况有一个很好的反省:

var foo = [[String]]()
foo.append(["bar"])
foo[0][0] // reveals "bar"

It means it's an array of arrays of messages. 这意味着它是一组消息数组。 Think of it in terms of whatever appears between the square brackets being an array of that, and this can include another array. 可以根据方括号之间出现的任何内容来考虑它,这可以包括另一个数组。

Alternatively, if you were to write out without the “shorthand” array syntax, it would be Array<Array<Messages>>() . 或者,如果您在没有“简写”数组语法的情况下写出来,那么它将是Array<Array<Messages>>()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM