I’m a newbie, so I wouldn’t be surprised if it turned out to be something stupid, but I’m banging my head against a wall trying to figure out why the NSArray haikuArray, after I try to initWithObjects, never contains any objects. Any thoughts?
Gay_HaikuViewController.h file:
#import <UIKit/UIKit.h>
#import "Array Setup.h"
@interface Gay_HaikuViewController : UIViewController
{
Array_Setup *gayHaikuSetup;
Array_Setup *userHaikuSetup;
NSMutableArray *allHaiku;
IBOutlet UITextView *haikuText;
}
@property (nonatomic,retain) Array_Setup *gayHaikuSetup;
@property (nonatomic,retain) Array_Setup *userHaikuSetup;
@property (nonatomic,retain) NSMutableArray *allHaiku;
@property (nonatomic,retain) IBOutlet UITextView *haikuText;
-(IBAction)nextHaiku:(id)sender;
Relevant method in Gay_HaikuViewController.m file:
#import "Gay_HaikuViewController.h"
#import "Array Setup.h"
@interface Gay_HaikuViewController ()
@end
@implementation Gay_HaikuViewController
@synthesize gayHaikuSetup,userHaikuSetup,allHaiku,haikuText;
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *haikuArray = [gayHaikuSetup arrayOfGayHaiku];
haikuArray = [gayHaikuSetup readFromGayArray];
//This next line is how I know there are no objects in haikuArray.
NSLog(@"%d",[haikuArray count]);
NSMutableArray *userArray = [userHaikuSetup arrayOfUserHaiku];
userArray = [userHaikuSetup readFromUserArray];
allHaiku = [[NSMutableArray alloc] init];
for (int i=0; i<[haikuArray count]; i++)
[allHaiku addObject:[haikuArray objectAtIndex:i]];
for (int i=0; i<[userArray count]; i++)
[allHaiku addObject:[userArray objectAtIndex:i]];
}
Array_Setup.h file:
#import <Foundation/Foundation.h>
@interface Array_Setup : NSObject
{
NSMutableArray *arrayOfUserHaiku;
NSArray *arrayOfGayHaiku;
}
@property (nonatomic,strong) NSMutableArray *arrayOfUserHaiku;
@property (nonatomic,strong) NSArray *arrayOfGayHaiku;
-(NSArray *)readFromGayArray;
-(NSMutableArray *)readFromUserArray;
-(NSMutableArray *)writeToUserArray;
-(void)addHaikuToArray;
@end
And the relevant methods in Array_Setup.m file:
#import "Array Setup.h"
@implementation Array_Setup
@synthesize arrayOfGayHaiku,arrayOfUserHaiku;
-(NSMutableArray *)readFromUserArray
{
arrayOfUserHaiku = [NSMutableArray alloc];
[arrayOfUserHaiku initWithContentsOfFile:@"/tmp/cool.txt"];
NSError *error = nil;
if (!arrayOfUserHaiku)
{
NSLog(@"Read failed: %@.",[error localizedDescription]);
}
else
{
NSLog(@"cool.txt looks like this: %@.",arrayOfUserHaiku);
}
return arrayOfUserHaiku;
}
-(NSArray *)readFromGayArray
{
NSArray *array = [NSArray alloc];
[array initWithObjects:@"Lorem", @"ipsum", nil];
return array;
}
@end
Thanks in advance for your help, guys and gals.
Serious problems in this code:
Never split the
allocand theinit.initmethods will sometimes (often, actually) not return the same object thatallocreturned. I.e. that should be:arrayOfUserHaiku = [[NSMutableArray alloc] initWithContentsOfFile:@”/tmp/cool.txt”];
errorisn’t going to be set by anything in that code ever; it isn’t a global.That method should return an autoreleased array (assuming non-ARC).
You should avoid large arrays of static strings in your code. Put ’em into the app wrapper as a plist or text file that can be localized easily.
(And, as the commenter suggested, don’t put your app’s contents into the question — some might find it offensive)