PDA

View Full Version : Passing an array into a method




zcarter
Mar 17, 2008, 07:23 PM
How can I pass an array into a method.

For example:

If i had this array..

array[0] = @"Info";
array[1] = @"Info2";
array[2] = @"Info3";

and I have the method

- (NSString *)DisplayArray:(NSString *)thevalue
{

How could I properly write "thevalue" as declared in the parameter above be set to take in an array?

What I want out of this is to have the entire array passed into thevalue and to return one piece of the array..

so like

return thevalue[2];

or return the value[0];


Do you follow me?

Hope so!

lemme know - thanks a lot!

- Zac

}



Muncher
Mar 17, 2008, 07:33 PM
How can I pass an array into a method.

For example:

If i had this array..

array[0] = @"Info";
array[1] = @"Info2";
array[2] = @"Info3";

and I have the method

- (NSString *)DisplayArray:(NSString *)thevalue
{

How could I properly write "thevalue" as declared in the parameter above be set to take in an array?

What I want out of this is to have the entire array passed into thevalue and to return one piece of the array..

so like

return thevalue[2];

or return the value[0];


Do you follow me?

Hope so!

lemme know - thanks a lot!

- Zac

}

I know you're using ObjC, but in C, you would probably want to pass a pointer to the array. I think that would be the simplest way to do it.

In C this would look like:

int array[20];
...
int* DisplayArray(int *array) {
...*array[i]...
}

There's probably a better way in ObjC, but I don't know that language so... here you go!

zcarter
Mar 17, 2008, 07:37 PM
Yeah,

Thanks for the quick reply...the problem is, I don't know the proper syntax for doing it in Obj. C

-Zac

antibact1
Mar 17, 2008, 07:46 PM
The trick with Objective-C is always to think of things as objects (unless of course there is a great reason not to). There is a nice type, NSArray, that can do exactly what you want.

@implementation TestClass

- (id)init
{
self = [super init];

NSArray *array = [[NSArray alloc] initWithObjects:@"first", @"second", @"third", nil];
[self displayArray:array];

return self;
}

- (void)displayArray:(NSArray *)anArray
{
NSLog(@"%@", [anArray objectAtIndex:0]);
NSLog(@"%@", [anArray objectAtIndex:1]);
NSLog(@"%@", [anArray objectAtIndex:2]);
}

@end

Edit: There is also the type NSMutableArray that you may want to check out.

zcarter
Mar 17, 2008, 07:53 PM
Thanks a lot for the quick reply!

Can you also explain to me how NSLog works and what it is with the iPhone?

- Zac

gnasher729
Mar 17, 2008, 07:54 PM
It looks like you have an array of NSString*, for example

NSString* myArray [3];

You can declare an array as a function parameter either by writing

NSString* theValues []

or

NSString** theValues

and then you pass myArray as the argument.

(If you know C, or C++: It is exactly the same as passing an array of pointers in these languages).

zcarter
Mar 17, 2008, 07:59 PM
That sounds easy...

but when I do

- (void)DisplayArray : (NSString *)theValues[]


I get an error...


Why?


-Z

antibact1
Mar 17, 2008, 08:06 PM
That sounds easy...

but when I do

- (void)DisplayArray : (NSString *)theValues[]


I get an error...


Why?


-Z

Use NSArray or NSMutableArray

antibact1
Mar 17, 2008, 08:09 PM
Thanks a lot for the quick reply!

Can you also explain to me how NSLog works and what it is with the iPhone?

- Zac

NSLog logs a message to stderr. It is the same thing on the iPhone and logs a message to the console.

Based on your previous comments, I'm guessing you have no experience developing on OS X and are trying to jump straight to iPhone development. While this sounds like a great idea, you may want to try some straight OS X development first to get familiar with how everything fits together.

zcarter
Mar 17, 2008, 08:09 PM
Hey...

One more question, then im set I think..

I've been trying to save my data, and in order to so you need convert what you have to NSData.

Apple wrote a function to that for you on their site, however, I cannot convert back from NSData to NSArray..

Any ideas how?

Thanks,

Zac

HiRez
Mar 17, 2008, 08:10 PM
It looks like you have an array of NSString*, for example

NSString* myArray [3];

You can declare an array as a function parameter either by writing

NSString* theValues []

or

NSString** theValues

and then you pass myArray as the argument.

(If you know C, or C++: It is exactly the same as passing an array of pointers in these languages).You probably wouldn't normally do that in Cocoa. As antibact1 says, you would declare an NSArray object, add your NSString objects into the array, then simply pass the array reference to the function.-(void)displayArray:(NSArray*)anArray {
NSObjectEnumerator *e = [anArray objectEnumerator];
NSString *aString = nil;
while (aString = [e nextObject]) {
// only do this if you're sure all the objects in your array are valid NSStrings.
NSLog(aString);
}
}

// then to call it, in this case the method was declared in some object called "myController"
NSArray *myArray = [NSArray arrayWithObjects: @"Info", @"Info2", @"Info3", nil];
[myController displayArray:myArray];

HiRez
Mar 17, 2008, 08:22 PM
Hey...

One more question, then im set I think..

I've been trying to save my data, and in order to so you need convert what you have to NSData.

Apple wrote a function to that for you on their site, however, I cannot convert back from NSData to NSArray..You probably want to use NSArchiver and NSUnarchiver (and related classes like NSCoder). NSStrings know how to create themselves from NSData objects, and turn themselves into NSData objects. As well, NSData objects can do the opposite. Just check out the docs for those objects.

I really would recommend you start by reading through a book to learn Cocoa, though, preferably Aaron Hillegass's (http://www.amazon.com/Cocoa-Programming-Mac-OS-3rd/dp/0321503619/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1205803343&sr=1-1).

antibact1
Mar 17, 2008, 08:28 PM
Hey...

One more question, then im set I think..

I've been trying to save my data, and in order to so you need convert what you have to NSData.

Apple wrote a function to that for you on their site, however, I cannot convert back from NSData to NSArray..

Any ideas how?

Thanks,

Zac

If you have written it to a file and want to read it in, you can use [NSArray initWithContentsOfFile:filename]

Otherwise, try NSKeyedArchiver and NSKeyedUnarchiver

lee1210
Mar 17, 2008, 08:28 PM
Because I know very little Obc-C I wanted to try this out, too:

main.m

#import <Foundation/Foundation.h>
#include "Worker.h"

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSArray *array = [NSArray arrayWithObjects:@"Info",@"Info2",@"Info3", nil];

NSLog([[Worker DisplayArray:array] objectAtIndex: 1]);
[pool release];
return 0;
}

worker.m

#import "Worker.h"


@implementation Worker
+ (NSArray *)DisplayArray:(NSArray *)thevalues
{
NSRange theRange;

theRange.location = 1;
theRange.length = 2;
return [thevalues subarrayWithRange:theRange];
}
@end


worker.h

@interface Worker : NSObject {
}
+ (NSArray *)DisplayArray:(NSArray *)thevalues;

@end


I didn't know if you wanted ONE string or a range from the array. One piece is ambiguous. Changes to DisplayArray would be trivial to change to a single NSString * being returned.

-Lee

zcarter
Mar 17, 2008, 08:47 PM
Yeah I got that part down..

My two biggest issues now are..

#1 - I cannot for some reason save my array and read back in from it. (I have now figured out how to properly pass the array as well..so as you can see below, it "Trys" to grab the array and pass it in as a parameter like this: [contentView DisplayArray: read])

My code is as follows:

//Write into File Example
NSArray *thenew = [[NSArray alloc] initWithObjects:@"first", @"second", @"third", nil];
[thenew writeToFile:@"Test.txt" atomically:NO];

//Read from the file example
NSArray *read;
[read initWithContentsOfFile:@"Test.txt"];

//Display the Label
[contentView DisplayArray:read];


As you can see..read is supposed to be read in from the same file (Im assuming it doesn't matter the name of the file? Also..does it auto create the file if its not there? Where Can i view these files once they have been written.

And Simply #2...

How do you use NSLog?

Lol..


Your help is always appreciated...

kainjow
Mar 17, 2008, 08:59 PM
//Read from the file example
NSArray *read;
[read initWithContentsOfFile:@"Test.txt"];

As you can see..read is supposed to be read in from the same file (Im assuming it doesn't matter the name of the file? Also..does it auto create the file if its not there? Where Can i view these files once they have been written.

initWithContentsOfFile: is a method you use on an instance of NSArray. You have not yet created a new instance of NSArray. So your code should look like this:

NSArray *read = [[NSArray alloc] initWithContentsOfFile:@"Test.txt"];

How do you use NSLog?

NSLog() formats strings and writes it to the standard output. For example:

NSString *name = @"Bobby";
int age = 15;
NSLog(@"Hello there, %@. You are %d years old.", name, age);

Which would output to the console:

Hello there, Bobby. You are 15 years old.

Check out String Format Specifiers (http://developer.apple.com/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html) for more ways on how you can use other data types with NSLog() (and other similar methods).