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.
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 ).
@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.
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.
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.
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.