Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-21-2010, 11:01 AM   #1 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 5
jpburns is on a distinguished road
Default Best place to populate arrays - passing values?

Hello:

Total newbie, just learning Obj C, but want to start by recreating something I did in JavaScript, basically a random Haiku generator, but am having problems figuring out when and where to populate an array of strings, and then how to access them, or perhaps, pass their values.

I understand that awakeFromNib is a good place to do this, but if I fill my arrays there, they're just local, and need the values available to other methods. If I first declare the array (I guess, making it an instance variable), and then later fill it in a method, it seems that garbage can get in the array, and I seem to be reading random values when I spit them out.

Here's the relevant snippet of code:

Code:
 -(id)returnHaiku
    {
    	return [self getOne:[self getOne:five]] // doesn't know what "five" is...
    }
    
    - (void)awakeFromNib
    
    {
    	[super awakeFromNib];
    	[self fillArrays];	
    }
    
    -(id)fillArrays
    {	
    	NSArray *five=[NSArray arrayWithObjects: @"blustry zen buddhists",@"barking tiny dogs", @"closeted bigot", @"yowling alley cats",@"shrugging teenagers",@"piece of tangled string", @"ball of woolen yarn", @"big pile of garbage",  @"line of well-wishers", @"moldy piece of bread", @"middle manager", @"a terrified rat", @"whispering goofballs", @"various people", @"cross-dressing monkey", @"terrifying dolt", @"sneering idiot", @"grinning sycophant", @"hurtful sloganist",@"annoying haiku",@"hardened criminal",@"vile politician", @"lost generation", @"poetical crap",@"slimy strategist", @"fake conservative", @"old-style liberal",@"evil yuppie scum", @"proud midwesterner",@"artful panhandler",@"noisy spoiled brats",@"frustrated poseurs",nil];
    	return five;
    }
    
    -(id)getOne:(NSArray *)myArray
    {
    	return [[myArray objectAtIndex:arc4random()%myArray.count]stringByAppendingString:@"\n"];
    }
I realize that I'm just throwing away the returned value, but can't seem to figure out the right way to deal with this.

I'd appreciate any help you could provide this newbie. Thanks in advance.
jpburns is offline   Reply With Quote
Old 05-21-2010, 11:52 AM   #2 (permalink)
A Single-Serving Friend
 
Join Date: Mar 2010
Location: Groningen, NL
Posts: 491
Robert Paulson is on a distinguished road
Default

Hi there and welcome!

I'd recommend to use an instance variable. This is how I usually go about solving an issue like yours (which doesn't mean it's the best solution since I am a newbie as well ).

In you .h file:

Code:
@interface FooViewController : UIViewController {

	NSArray *five;
}

@property (nonatomic, retain) NSArray *five;
And in your .m file:

Code:
@synthesize five;

-(void) viewDidLoad {

	five = [[NSArray alloc] initWithObjects: ... ]; // all your objects go here
}
Now, you can access "five" from all the other methods in your .m file. (Don't forget to release five in your dealloc!)

I don't quite understand the logic of your other methods to be honest. But could you do something like this:

Code:
-(NSString *)getOne
    {
    	return [NSString stringWithFormat: @"%@\n", [five objectAtIndex:arc4random() % five.count]];
    }

-(void) createHaiku {

	NSString *haiku = [self getOne];  // Get the first line

	[haiku stringByAppendingString: [self getOne]]; // Get the second line
	[haiku stringByAppendingString: [self getOne]]; // Get another random line, etc.

	NSLog(haiku);  // Print the haiku to the console.
}
I am not at home right now so this code is not tested. Sorry if there are mistakes but I hope you can follow me, hehe.

Hope this helps.

Cheers,
Bob
__________________
We are God’s middle children, according to Tyler Durden, with no special place in history and no special attention.

Consider saying thanks by buying my app. :]

Last edited by Robert Paulson; 05-21-2010 at 12:01 PM.
Robert Paulson is offline   Reply With Quote
Old 05-21-2010, 12:06 PM   #3 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 5
jpburns is on a distinguished road
Default

See, I tried that, but I don't understand some of the additional stuff in both files...

Specifically, in the .h file:

Code:
@property (nonatomic, retain) NSArray *five;
What is that line actually doing?

and in the .m file:

Code:
@synthesize five;

-(void) viewDidLoad {

	five = [[NSArray alloc] initWithObjects: ... ]; // all your objects go here
}
So what's the "@synthesize five" line doing?

Also: Why do i have to memory manage the NSArray here? I thought that if I used a "factory method" like initWithObjects, then I didn't have to manage that chunk of memory. Maybe I'm not understanding that part.

I did something earlier where I was declaring the instance variable in the .h file:
Code:
NSArray *five;
and then filling it in the .m file,

Code:
five = [NSArray arrayWithObjects: @"blustry zen buddhists"...] // and more, omitted here...)
but I would get a EXEC crash when I tried to access an individual element from it. Appreciate any illumination you can provide.
jpburns is offline   Reply With Quote
Old 05-21-2010, 01:15 PM   #4 (permalink)
A Single-Serving Friend
 
Join Date: Mar 2010
Location: Groningen, NL
Posts: 491
Robert Paulson is on a distinguished road
Default

It'd take a while to explain all that and it's very basic stuff that google can tell you all about. BrianSlick (a forum member here) wrote a great guide on properties (it's in his signature), check it out.

Also, you should read some basics about memory management and the difference between autoreleased objects and those you alloc yourself.

I cannot really recommend any sources on the 'net... I read Kochan's Programming in Objective-C 2.0 which explains all the basics. AFAIK, there are some introductory texts on Apple's 'site as well.

Good luck and don't give up!

Cheers,
Bob
__________________
We are God’s middle children, according to Tyler Durden, with no special place in history and no special attention.

Consider saying thanks by buying my app. :]
Robert Paulson is offline   Reply With Quote
Old 05-27-2010, 08:43 AM   #5 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 5
jpburns is on a distinguished road
Default

OK.

Answering my own question, but maybe this will help someone else out.

Here's the relevant code, what I ended up with:

Code:
-(id)returnHaiku
{
	return [self getOne:five];
}

- (void)awakeFromNib

{
	[super awakeFromNib];
	
	[self fillArrays];	
}

-(void)fillArrays
{
//fill array declared in header file as an instance variable

//Note: it's using arrayWithObjects, a factory method, which I guess 
// autoreleases memory after the array is no longer needed...
	
five=[NSArray arrayWithObjects: @"blustry zen buddhists",@"barking tiny dogs", @"closeted bigot", @"yowling alley cats",@"shrugging teenagers",@"piece of tangled string", nil];

// and here's what I was missing!!
	[five retain];
}

-(id)getOne:(NSArray *)myArray
// returns random element from an array
{
	return [[myArray objectAtIndex:arc4random()%myArray.count]stringByAppendingString:@"\n"];
}
So my earlier problem apparently involved not retaining the array I was filling. I guess it was being dumped right after being passed out of the method, and so couldn't be used in the later method that picked a random object out of the array.

This works fine.
jpburns is offline   Reply With Quote
Reply

Bookmarks

Tags
arrays, awakefromnib, obj c, scope

Thread Tools
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
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 464
14 members and 450 guests
7twenty7, AlanFloyd, David-T, imac74, Jaxen66, logan, lovoyl, Music Man, mutantskin, Sami Gh, SLIC, solardrift, unicornleo, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,683
Threads: 94,131
Posts: 402,932
Top Poster: BrianSlick (7,990)
Welcome to our newest member, unicornleo
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:09 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0