Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

zcarter

macrumors member
Original poster
Apr 21, 2007
46
0
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

macrumors 65816
Apr 19, 2007
1,465
0
California
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:

Code:
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

macrumors member
Original poster
Apr 21, 2007
46
0
Yeah,

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

-Zac
 

antibact1

macrumors 6502
Jun 1, 2006
334
0
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.

Code:
@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

macrumors member
Original poster
Apr 21, 2007
46
0
Thanks!

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

Suspended
Nov 25, 2005
17,980
5,565
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

macrumors member
Original poster
Apr 21, 2007
46
0
That sounds easy...

but when I do

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


I get an error...


Why?


-Z
 

antibact1

macrumors 6502
Jun 1, 2006
334
0
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

macrumors member
Original poster
Apr 21, 2007
46
0
Great thanks guys!

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

macrumors 603
Jan 6, 2004
6,250
2,576
Western US
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.
Code:
-(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

macrumors 603
Jan 6, 2004
6,250
2,576
Western US
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.
 

antibact1

macrumors 6502
Jun 1, 2006
334
0
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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Because I know very little Obc-C I wanted to try this out, too:

main.m
Code:
#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
Code:
#import "Worker.h"


@implementation Worker
+ (NSArray *)DisplayArray:(NSArray *)thevalues
{
  NSRange theRange;
 
  theRange.location = 1;
  theRange.length = 2;
  return [thevalues subarrayWithRange:theRange];
}
@end

worker.h
Code:
@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

macrumors member
Original poster
Apr 21, 2007
46
0
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:
Code:
	//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

Moderator emeritus
Jun 15, 2000
7,958
7
Code:
//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:

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

zcarter said:
How do you use NSLog?

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

Code:
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 for more ways on how you can use other data types with NSLog() (and other similar methods).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.