Jag har skrivit ett program men funderar kring användningen av alloc, init och release. Gör det någon skillnad i till exempel det här exemplet?
Jag har den här koden:
#import "QuoteController.h"
@implementation QuoteController
- (IBAction)SaySomething:(id)sender
{
/* Create an array to contain the quotes */
NSMutableArray *myArray = [NSMutableArray array];
/* Fill the array with quotes */
[myArray addObject:@"Me Fail English? That's Unpossible! - Ralp Wiggum"];
[myArray addObject:@"Kids, you tried your best and you failed miserably. The lesson is, never try. - Homer Simpson"];
[myArray addObject:@"The doctor said I wouldn't have so many nose bleeds if I kept my finger outta there. - Ralp Wiggum"];
/* Get a random index number */
int index = (int)([myArray count]*drand48());
/* Put the quote in a string */
NSString *myQuote = [myArray objectAtIndex:index];
NSString *myTitle = [NSString stringWithFormat:@"Quote number %d", index+1];
/* Show the quote */
NSRunAlertPanel(myTitle, myQuote, nil, nil, nil);
}
@end
Jag har den även omskriven till:
#import "QuoteController.h"
@implementation QuoteController
- (IBAction)SaySomething:(id)sender
{
/* Create an array to contain the quotes */
NSMutableArray *myArray = [NSMutableArray array];
/* Fill the array with quotes */
[myArray addObject:@"Me Fail English? That's Unpossible! - Ralp Wiggum"];
[myArray addObject:@"Kids, you tried your best and you failed miserably. The lesson is, never try. - Homer Simpson"];
[myArray addObject:@"The doctor said I wouldn't have so many nose bleeds if I kept my finger outta there. - Ralp Wiggum"];
/* Get a random index number */
int index = (int)([myArray count]*drand48());
/* Put the quote in a string */
NSString *myQuote = [[NSString alloc] initWithString:[myArray objectAtIndex:index]];
NSString *myTitle = [[NSString alloc] initWithString:[NSString stringWithFormat:@"Quote number %d", index+1]];
/* Show the quote */
NSRunAlertPanel(myTitle, myQuote, nil, nil, nil);
[myQuote release];
[myTitle release];
}
@end