Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > NSMutableArray

NSMutableArray
Thread Tools
Fresh-Faced Recruit
Join Date: May 2002
Status: Offline
Reply With Quote
May 19, 2002, 03:17 PM
 
I have a program where I need to use arrayWithContentsOfFile in awakeFromNib. I also need to use writeToFile:atomically: in an action. My problem is that if I declare the mutable array in the .h file it won't let me use arrayWithContentsOfFile to initialize it. I just need a mutable array available to any function in a .m file.
     
Forum Regular
Join Date: Oct 2001
Location: Sweden
Status: Offline
Reply With Quote
May 19, 2002, 03:59 PM
 
Originally posted by everdream:
<STRONG>I have a program where I need to use arrayWithContentsOfFile in awakeFromNib. I also need to use writeToFile:atomically: in an action. My problem is that if I declare the mutable array in the .h file it won't let me use arrayWithContentsOfFile to initialize it. I just need a mutable array available to any function in a .m file.</STRONG>
NSMutableArray is a subclass of NSArray, and implements all methods from NSArray. Therefor you should have no problem calling:
myMutableArray = [NSMutableArray arrayWithContentsOfFile:...];
to get a mutable array back. What problem is preventing you from this? Is the compiler complaining about something? (it shouldn't)
Just remember that arrayWithContentsOfFile: returns an autoreleased object, so if you need to access outside the awakeFromNib method you need to call [[NSMutableArray arrayWithContentsOfFile:...] retain] or
[[NSMutableArray alloc] initWithContentsOfFile:...]. Remember to call [myMutableArray release] somewhere to avoid leaking the memory. The dealloc method is probably a good place for this. Hope this helps!

/Tobias
     
Forum Regular
Join Date: Mar 2002
Location: Düsseldorf, Germany, Europe, Earth
Status: Offline
Reply With Quote
May 19, 2002, 05:00 PM
 
To clarify, you declare your array by
NSMutableArray *myArray;
in your .h file. Then you can allocate and fill it with
myArray = [NSMutableArray arrayWithContentsOfFile:blablabla];
as Tobias mentioned.

HTH, Seb.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:02 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2