0

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.

7
  • In swift, Array is copy-on-write. That means whenever you pass an array the array is copied. So you don't really need to worry about the new instance. Commented Apr 9, 2019 at 9:33
  • @nayem Not sure how the garbage collector in Swift works but it's more that I don't want to create a new var that I'm never going to use. I guess I'm just used to passing the new array directly. Is there then even a difference between creating a new var and creating and passing the array directly in Swift? Commented Apr 9, 2019 at 9:42
  • You've asked these two questions, right? 1. creating a new var, 2. creating and passing the array directly. And you want to know if there is any difference between them or not. If I'm correctly getting into your question, then no, there isn't any difference. Essentially when you pass an array to another method, a complete new instance is actually passed. Commented Apr 9, 2019 at 9:52
  • 1
    Well, you are thinking too much about it. There are fundamental differences between Java and Swift. Java sees any object as reference but Swift doesn't always. You need to study the differences between value & reference more in Swift. Swift is designed keeping value types in mind. You see a lot of struct's in Swift. Commented Apr 9, 2019 at 10:20
  • 1
    Now if you just want to know the answer to your question, here it is: myFunction([myclass1, myclass2, myclass3]). You bracket a bunch of object using [ ], you create an instance of Array. That's it. Commented Apr 9, 2019 at 10:23

1 Answer 1

3

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")])
Sign up to request clarification or add additional context in comments.

4 Comments

I tried that too but it doesn't work (the type is "myclass", I just used String to make it easier to see). Instead it says: Cannot convert value of type ‚(String, String, String)‘ to expected element type ‚String‘.
@Neph Without seeing the actual code it is hard to help you but it looks like you are using parentheses like some tuple?
No tuples. I'm going to edit my question a bit more, hopefully it's a bit clearer then: I've got a class MyClass and created a bunch of instances that carry data. These instances I want to add to an array, which I'm then using to create an instance of a second class (MyOtherClass - including some Strings), which in turn I'm passing from my main View to a second View via a segue.
Thanks. I must have had some other brackets in there that Xcode didn't recognize as culprit, with [myclass1, myclass2, myclass3] it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.