Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 17, 2008, 07: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, 07: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, 08: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, 08: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, 08: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

Posting 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

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Passing id to parameter of incompatible type 'float' johnmerlino Mac Programming 12 Nov 19, 2011 08:37 PM
Passing NSArray from NSOperation to caller arnieterm iPhone/iPad Programming 3 Nov 28, 2010 11:06 PM
Issue Passing NSMutableDictionary to Method roswell iPhone/iPad Programming 1 Apr 4, 2010 02:35 PM
How do we pass NSArray as reference anni.saini Mac Programming 14 Dec 29, 2009 01:41 AM
nsarray to nsdata BollywooD Mac Programming 3 Oct 25, 2009 03:43 PM


All times are GMT -5. The time now is 08:55 PM.

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

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