Reputation: 2001
In e.g. Java you can do something like this:
myFunction(new MyClass[]{myclass1, myclass2, myclass3})
Is there an equivalent in Swift?
I tried
myFunction([MyClass](myclass1,myclass2,myclass3))
and Xcode suggested to change it to
myFunction([MyClass](arrayLiteral: myclass1,myclass2,myclass3))
but the documentary (click) tells you not to call "arrayLiteral" directly.
Edit: The reason why I want to do this is a bit complicated:
I've got a class MyClass
and created a bunch of instances that carry data: myclass1, myclass2, myclass3
init(name na:String, number nu:Int, content c:String) {....}
These instances I want to add to an array, which I'm then using to create an instance of a second class MyOtherClass
:
init(name n:String, someotherinfo s:String, myclassarray m:[MyClass]) {....}
Creating the instance:
var myotherclassthing = MyOtherClass(name:"Test", someotherinfo:"Bla", myclassarray: ??????????)
This instance of MyOtherClass
I'm then passing from my main View to a second View via a segue.
Upvotes: 0
Views: 1226
Reputation: 52088
This should work
myFunction(["a","b","c"])
This works equally well if you want to return an array from a func
func test() -> [String] {
return ["a", "b"]
}
And it works equally well with a custom class
MyOtherClass(name:"Test", someotherinfo:"Bla",
myclassarray: [MyClass(name: "A", number: 1, content: "AAA"),
MyClass(name: "B", number: 2, content: "BBB")])
Upvotes: 3