我是一个新手,所以如果结果是愚蠢的话,我不会感到惊讶,但是我试图弄清楚为什么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
提前感谢您的帮助,伙伴和女孩们。
答案 0 :(得分:1)
initWithObjects
会返回一个值,但您没有将array
设置为返回值。
答案 1 :(得分:0)
此代码中的严重问题:
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;
}
切勿拆分alloc
和init
。 init
方法有时(通常实际上)不返回alloc
返回的同一对象。即应该是:
arrayOfUserHaiku = [[NSMutableArray alloc] initWithContentsOfFile:@“/ tmp / cool.txt”];
error
不会被该代码中的任何内容设置;它不是一个全球性的。
该方法应返回自动释放的数组(假设为非ARC)。
您应该在代码中避免使用大型静态字符串数组。将它们作为可以轻松本地化的plist或文本文件放入app包装器中。
(而且,正如评论者建议的那样,不要将你的应用内容放入问题中 - 有些人可能会发现它具有攻击性)
答案 2 :(得分:-1)
Array_Setup.m文件:
#import "Array_Setup.h"
@implementation Array_Setup
@synthesize arrayOfGayHaiku,arrayOfUserHaiku;
-(NSMutableArray *)readFromUserArray
{
arrayOfUserHaiku = [[NSMutableArray alloc] initWithContentsOfFile:@"/tmp/cool.txt"];
// [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 arrayWithObjects:@"I don't understand.\nYou love it when I do that--\nWait, no. That's Stephen.",
@"Frantically hiding\nPorno and Mapplethorpe prints--\nMom is on her way.",
@"Remember when I\nSaid I disliked oral sex?\nI meant just with you.",
@"You were perfection.\nThen you misspelled 'embarrassed.'\nDon't call me again.",
//Am omitting a bunch of these to make my Stack Overflow question shorter.
@"I know you think I\nLike it when you slap my ass.\nYou are mistaken.",
@"Flailing to the beat--\nCould I possibly look half\nAs dumb as I feel?",
@"Last night, you looked hot.\nToday, you want to discuss\nThe whole foods movement.", nil];
return array;
}
@end
Gay_HaikuViewController.m文件:
userHaikuSetup= [[Array_Setup alloc] init];
gayHaikuSetup= [[Array_Setup alloc] init]; // [gayHaikuSetup arrayOfGayHaiku];
NSArray * haikuArray = [NSArray arrayWithArray: [gayHaikuSetup readFromGayArray]];
//This next line I added to find out whether the array had any objects--I always get 0.
NSLog(@"%lu",[haikuArray count]);
NSMutableArray *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]];
NSLog(@"%lu",[allHaiku count]);
这可以运行;你应该看到一些objective-c程序语法书。