Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does double square brackets mean in swift?

the below is the sample code in swift.

var loadedMessages = [[Message]]()

Message is a custom class. i'm not sure what [[Message]] () is doing.

like image 473
shle2821 Avatar asked Dec 06 '22 22:12

shle2821


1 Answers

It is specifying that your variable loadedMessages is an array of arrays that contains Message objects. A JSON representation of loadedMessages might look like:

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

A quick Playground implementation of something similar can give you a pretty good introspection of the situation:

var foo = [[String]]()
foo.append(["bar"])
foo[0][0] // reveals "bar"
like image 56
Ian MacDonald Avatar answered Dec 24 '22 01:12

Ian MacDonald