0

I have to pass the following object to a method, have that method modify it so that after the method call I can move on with my modified object.

AVMutableCompositionTrack *compositionVideoTrack[2];

passing it like this currently:

[self buildShow:&compositionVideoTrack];

the buildshow method looks like this:

-(void)buildShow:(AVMutableCompositionTrack**)videoTracks{
}

I am getting this compiler warning and it is not currently working:

Incompatible pointer types sending 'AVMutableCompositionTrack *__strong (*)[2]' to parameter of type 'AVMutableCompositionTrack *__autoreleasing *'

How can I change this to make it work?

box86rowh
  • 3,415
  • 2
  • 26
  • 37
  • Don't use pass by reference unless it is absolutely necessary. Locke's answer below is a better fit with the design philosophies of the rest of the frameworks. – bbum Oct 25 '12 at 23:24
  • My plan is to have a few different objects that get modified by the method, and I will need to run the method a few times, so passing by ref works perfect for what I need, thanks for the comment! – box86rowh Oct 25 '12 at 23:44
  • If you have a few different objects that get modified by the method, a better design is to create a class that holds the various objects within and provides the logic for relating them. This will likely also be easier to refactor in the future (in that you change the class, not every single call site to the multi-arg-pass-by-reference-method). (If you look across the iOS/Cocoa APIs, pass-by-reference is used in a very limited fashion). – bbum Oct 26 '12 at 00:00

2 Answers2

2

Just pass it like this:

[self buildShow:compositionVideoTrack];

When you declare it like this:

AVMutableCompositionTrack * __autoreleasing compositionVideoTrack[2];

It's already an array of pointers, so it's compatible with the type of the parameter (AVMutableCompositionTrack**).

box86rowh
  • 3,415
  • 2
  • 26
  • 37
Jordão
  • 55,340
  • 13
  • 112
  • 144
  • Tried that, I get this error: Passing address of non-scalar object to __autoreleasing parameter for write-back – box86rowh Oct 25 '12 at 22:33
  • @box86rowh: look [here](http://stackoverflow.com/a/7422679/31158) for some info about that. – Jordão Oct 25 '12 at 22:39
  • OK, I used a combination of your answer + declared my object like this: AVMutableCompositionTrack * __autoreleasing compositionVideoTrack[2]; I will add that part to your answer and mark it as the answer. – box86rowh Oct 25 '12 at 22:43
0

Unless you're working with a property, you ought to do it like this (note that the method now returns the object of the same type):

method constructor:

-(AVMutableCompositionTrack *)buildShow:(AVMutableCompositionTrack *)videoTracks {
    // do something
    return newVideoTracks;
}

And, method call:

newVideoTracks = [self buildShow:compositionVideoTrack];

If you use a property, you can have a void method with no parameters.

Adis
  • 4,512
  • 2
  • 33
  • 40
  • I simplified my question for the purposes of posting here, my method will need to accept multiple objects so doing a return does not work. – box86rowh Oct 25 '12 at 22:38
  • You can always pass multiple parameters to a method. You might want to look into using NSArrays, or some other native component that will behave more nicely than only C code. – Adis Oct 26 '12 at 00:04