0

I have a Objective-C function which we'll call foo for the purposes of being useful to others.

something a //Define variable a as a 'something' type
[foo:&a]

How do I go about translating this into swift?

2
  • 2
    What is something? A scalar type, struct, union, class, ...? What does foo? How is the method declared? Does it modify the referenced element? – A concrete compiling example would be helpful. Commented Jun 1, 2015 at 17:42
  • Apologies for the vagueness. 'a' is a class in a SDK which foo, also part of the sdk, would reference to in this case return a modified class object containing error codes. Commented Jun 1, 2015 at 21:27

2 Answers 2

1

In swift if you want to pass by reference you use the inout notation in your function definition

such as

func test(inout a : Double) {
    //do something
}

var d : Double = 3.5
test(&d)

If you are referring to using a function directly you would use the same &variable notation you use elsewhere

So you would just call foo(&var)

Sign up to request clarification or add additional context in comments.

Comments

0

If the something is an instance of a class, it is automatically pass by reference when you do:

foo(a)

If the something is an instance of a struct, it's passed in as a copy and you shouldn't try to pass it's address around. Instead make something a class.

Comments

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.