Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 17, 2008, 08:13 AM   #1
neil.b
macrumors member
 
Join Date: Nov 2008
Passing NSArray to a method?

I'm sure I'm missing something stupidly obvious but I'm trying to pass a NSArray to the init method of my class;

Code:
NSArray *someStuff[3] = {@"name 1", @"name 2",  nil};

MyClass *myInstance = [[MyClass alloc] initWithStuff:*someStuff];
And in the class;

Code:
//Interface
-(id) initWithStuff:(NSArray *)array;

//Method
-(id) initWithStuff:(NSArray *)array
{
        NSString *aString;
        for (int i =0; i < 3; i++)
        {
                 aString = [array objectAtIndex:i];
                 // do some stuff with aString
        }
        return self;
}
Which causes a crash in the program in the for...loop. I know there's something fundamentally wrong but I can't see it and I can't see how to do it differently. Can anyone give me some pointers (sorry, bad pun)?
neil.b is offline   0 Reply With Quote
Old Dec 17, 2008, 08:36 AM   #2
detz
macrumors 65816
 
Join Date: Jun 2007
First, init your array like this.

PHP Code:
NSArray *someStuff = [NSArray arrayWithObjects:@"name 1", @"name 2"nil]; 
NSArray is an object.

When you're passing objects to methods they are pointers already so you don't have to add the * in there.

PHP Code:
MyClass *myInstance = [[MyClass allocinitWithStuff:someStuff]; 
And lastly in your for loop you should probably do

PHP Code:
for (int i =0< [someStuff count]; i++) 
So it can accept any number of elements.

Some simple NSLog's in between this code or a breakpoint would have told you that the array was not be allocated.
detz is offline   0 Reply With Quote
Old Dec 17, 2008, 09:08 AM   #3
neil.b
Thread Starter
macrumors member
 
Join Date: Nov 2008
What a dunce...

Thanks

Only problem is, I can't then define the array outside of a method or I get the error : "Initializer element is not a constant"?
neil.b is offline   0 Reply With Quote
Old Dec 17, 2008, 09:28 AM   #4
detz
macrumors 65816
 
Join Date: Jun 2007
Quote:
Originally Posted by neil.b View Post
What a dunce...

Thanks

Only problem is, I can't then define the array outside of a method or I get the error : "Initializer element is not a constant"?
NSArray *myArray;

.... In init

myArray = [[NSArray alloc] init.....
detz is offline   0 Reply With Quote
Old Dec 17, 2008, 09:38 AM   #5
neil.b
Thread Starter
macrumors member
 
Join Date: Nov 2008
That's what I'd done but I thought there might be a trick to defining the NSArray outside a method.

Is there no way of defining the array as a constant (outside the init method) and still be able to pass the array to my class method (and be able to index it)?

Thanks for the help, much appreciated.
neil.b is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Class Method -> Function -> Instance Method? Blakeasd Mac Programming 11 Jun 19, 2013 03:53 AM
NSArray to NSMutableArray larswik Mac Programming 3 Feb 19, 2013 11:10 PM
NSArray LastObject larswik iPhone/iPad Programming 4 Nov 5, 2012 08:01 PM
What is the difference between NSArray and NSMutableArray anandkumar45 iPhone Tips, Help and Troubleshooting 1 Nov 2, 2012 06:09 AM
(NSArray)writeToFile: issue mdeh Mac Programming 3 Jul 15, 2012 07:38 PM

Forum Jump

All times are GMT -5. The time now is 02:32 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC