Newbie array trouble

that sound again
Unregistered
 
Post: #1
Hi everyone! I'm a newbie having a little trouble with an array in cocoa.

While not strictly for a game, this code is part of a project that is eventually going to be a simple videomixer, so I'm using openGL and various other game-related techniques. I've learned a lot from reading this forum and thought I'd ask here because you seem like a friendly and knowledgeable group.

The problem is an array of 16 strings that I need to be available to a couple of methods in a class called mediaPicker. The array doesn't seem to hold any values, I can't even get an [object description], and the debugger can't read it. I know the paths below are OK, as I can display the images they point to in an NSImageView using the variable filePath.

In MediaPicker.h (after the interface part) i have this declaration:

Code:
NSArray *mediaFilePaths;
In Mediapicker.m, I first initialize the array thus in awakeFromNib:

Code:
[mediaFilePaths arrayWithCapacity: 16];
I've also tried:

Code:
mediaFilePaths = [[NSArray alloc] arrayWithCapacity: 16];
In another method I do:

Code:
NSString *filePath = [fileNames objectAtIndex:0];
Which gets filenames from drag'n'drop catcher. Then I go on to do:

Code:
[mediaFilePaths replaceObjectAtIndex: mediaCount withObject: filePath];
And in another method I call another object

Code:
[Controller mediaSelected: [mediaFilePaths objectAtIndex:number]];
As I said, the array doesn't seem to exist or hold any objects, regardless of which method I look in with the debugger. I'm coming from a more procedural background, and the problem is probably a really stupid mistake due to not understanding Obj-C / OO in full.

I've access to a couple of O'Reilly books on beginning Cocoa, but I haven't been able to find the answer there. A little nudge in the right direction as to where I might want to look would be appreciated.

Thanks!
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #2
Correct would be to allocate a mutable array:
PHP Code:
mediaFilePaths = [[NSMutableArray arrayWithCapacity16retain]; 
or
PHP Code:
mediaFilePaths = [[NSMutableArray allocinitWithCapacity16]; 
Also, you have to add objects before you can replace them, and so on. Well, everything is wrong. I suggest you read the Foundation reference docs about NSArray.
Quote this message in a reply
that sound again
Unregistered
 
Post: #3
Thanks, it's working now!

I just forgot to retain it. This memory management model is new to me (and something I'll have to study thoroughly in due course).

The array was changed from my original mutable one to the code you see above in the attempt to hunt down the error. I've tried all sorts of tricks, but now it works.

BTW, is it correct that it's only possible to declare your variables in the header file and not initialise them?

Thanks again, you made my day!
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #4
that sound again Wrote:BTW, is it correct that it's only possible to declare your variables in the header file and not initialise them?
No, that's incorrect. You can initialise them to any constant expression, which usually means you want your pointers to be nil, and properly initialise them when they are first used or during setup.
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #5
DoG Wrote:No, that's incorrect. You can initialise them to any constant expression, which usually means you want your pointers to be nil, and properly initialise them when they are first used or during setup.

Instance variables in an @interface block cannot be initialized in the header. That's what the -init method is for.

Global variables should not be defined in a header. If you need to do this for some reason, declare them as extern in the header, and define them in the implementation file.

- Alex Diener
Quote this message in a reply
DoG
Moderator
Posts: 869
Joined: 2003.01
Post: #6
ThemsAllTook Wrote:Instance variables in an @interface block cannot be initialized in the header. That's what the -init method is for.

Global variables should not be defined in a header. If you need to do this for some reason, declare them as extern in the header, and define them in the implementation file.

- Alex Diener
I believe he was referring to global vars. And those you need to declare in the header, and initialise/define in the implementation file.
Quote this message in a reply
that sound again
Unregistered
 
Post: #7
As you can probably see from the code above, my understanding of Cocoa and Obj-C is not that great. I was extremely cautious with where to post the original question, as some developer forums are extremely hostile to this type of newbie questions.

As far as my trouble with this program, much of it seems to stem from not properly understanding scope and memory management. So that's what I'm reading about now while merrily hacking away at different experiments. As it is, I'm producing horribly ugly code and probably have mem leaks all over the place, but that's OK as it helps me understand what's going on. One day I'll be able to implement my ideas properly, and your answers above have saved me a lot of time, simple as the solutions might seem. A big part of learning these new methods is knowing where to look.

DoG Wrote:I believe he was referring to global vars.
I'll have to get back to you on that once I'm completely sure myself Wink

So, once again, thanks for your time and the friendly atmosphere!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  2d array trouble saxplayer13 2 2,999 Nov 20, 2005 08:32 PM
Last Post: saxplayer13
  memcpy(stuct array pointer struct array point) unknown 22 10,865 Sep 29, 2005 03:16 PM
Last Post: unknown