我是一个新手,所以如果结果是愚蠢的,我不会感到惊讶,但我正用头撞墙试图弄清楚为什么 NSArray haikuArray,在我尝试 initWithObjects 之后,从不包含任何对象。有什么想法吗?
Gay_HaikuViewController.h 文件:
#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;
Gay_HaikuViewController.m 文件中的相关方法:
#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 文件:
#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
以及 Array_Setup.m 文件中的相关方法:
#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
提前感谢您的帮助,伙计们和女孩们。