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

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 17, 2008, 07:23 PM   #1
zcarter
macrumors member
 
Join Date: Apr 2007
Passing an array into a method

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 *)DisplayArrayNSString *)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

}
zcarter is offline   0 Reply With Quote
Old Mar 17, 2008, 07:33 PM   #2
Muncher
macrumors 65816
 
Muncher's Avatar
 
Join Date: Apr 2007
Location: California
Quote:
Originally Posted by zcarter View Post
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 *)DisplayArrayNSString *)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!
__________________
2.4Ghz 20" Rev. A iMac, Wii, crimson DS, intuos3 4x6, PS3
iPod touch 2G 16GB > Fiio E1 > Hifiman RE0
Muncher is offline   0 Reply With Quote
Old Mar 17, 2008, 07:37 PM   #3
zcarter
Thread Starter
macrumors member
 
Join Date: Apr 2007
Yeah,

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

-Zac
zcarter is offline   0 Reply With Quote
Old Mar 17, 2008, 07:46 PM   #4
antibact1
macrumors 6502
 
Join Date: Jun 2006
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.
antibact1 is offline   0 Reply With Quote
Old Mar 17, 2008, 07:53 PM   #5
zcarter
Thread Starter
macrumors member
 
Join Date: Apr 2007
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
zcarter is offline   0 Reply With Quote
Old Mar 17, 2008, 07:54 PM   #6
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
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).
gnasher729 is offline   0 Reply With Quote
Old Mar 17, 2008, 07:59 PM   #7
zcarter
Thread Starter
macrumors member
 
Join Date: Apr 2007
That sounds easy...

but when I do

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


I get an error...


Why?


-Z
zcarter is offline   0 Reply With Quote
Old Mar 17, 2008, 08:06 PM   #8
antibact1
macrumors 6502
 
Join Date: Jun 2006
Quote:
Originally Posted by zcarter View Post
That sounds easy...

but when I do

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


I get an error...


Why?


-Z
Use NSArray or NSMutableArray
antibact1 is offline   0 Reply With Quote
Old Mar 17, 2008, 08:09 PM   #9
antibact1
macrumors 6502
 
Join Date: Jun 2006
Quote:
Originally Posted by zcarter View Post
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.
antibact1 is offline   0 Reply With Quote
Old Mar 17, 2008, 08:09 PM   #10
zcarter
Thread Starter
macrumors member
 
Join Date: Apr 2007
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
zcarter is offline   0 Reply With Quote
Old Mar 17, 2008, 08:10 PM   #11
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: Western US
Quote:
Originally Posted by gnasher729 View Post
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];
__________________
Go outside, the graphics are amazing!

Last edited by HiRez; Mar 17, 2008 at 08:16 PM.
HiRez is offline   0 Reply With Quote
Old Mar 17, 2008, 08:22 PM   #12
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: Western US
Quote:
Originally Posted by zcarter View Post
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.
__________________
Go outside, the graphics are amazing!
HiRez is offline   0 Reply With Quote
Old Mar 17, 2008, 08:28 PM   #13
antibact1
macrumors 6502
 
Join Date: Jun 2006
Quote:
Originally Posted by zcarter View Post
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
antibact1 is offline   0 Reply With Quote
Old Mar 17, 2008, 08:28 PM   #14
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: 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
lee1210 is offline   0 Reply With Quote
Old Mar 17, 2008, 08:47 PM   #15
zcarter
Thread Starter
macrumors member
 
Join Date: Apr 2007
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...
zcarter is offline   0 Reply With Quote
Old Mar 17, 2008, 08:59 PM   #16
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
Quote:
Originally Posted by zcarter View Post
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"];
Quote:
Originally Posted by zcarter
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:

Quote:
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).
kainjow is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Who here is passing on the 5s and waiting for the 6? Seņor iPhone 77 Dec 16, 2013 04:09 AM
Apple should make a method that returns an array's object that satisfies a predicate. moonman239 iPhone/iPad Programming 5 Nov 21, 2013 07:21 PM
Class Method -> Function -> Instance Method? Blakeasd Mac Programming 11 Jun 19, 2013 02:53 AM
Trouble Passing Array to Another VC bkm iPhone/iPad Programming 8 Apr 19, 2013 04:48 PM
For those passing on iPhone 5... TG1 Alternatives to iOS and iOS Devices 60 Sep 13, 2012 10:36 AM

Forum Jump

All times are GMT -5. The time now is 06:32 PM.

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

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