-2

我是一个新手,所以如果结果是愚蠢的,我不会感到惊讶,但我正用头撞墙试图弄清楚为什么 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

提前感谢您的帮助,伙计们和女孩们。

4

3 回答 3

1

initWithObjects返回一个值,但您没有设置array为返回值。

于 2012-07-20T04:07:45.473 回答
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;
}
  1. 永远不要拆分allocinitinit方法有时(通常实际上)不会返回返回的相同对象alloc。即应该是:

    arrayOfUserHaiku = [[NSMutableArray alloc] initWithContentsOfFile:@"/tmp/cool.txt"];

  2. error永远不会被该代码中的任何内容设置;它不是全球性的。

  3. 该方法应该返回一个自动释放的数组(假设非 ARC)。

  4. 您应该避免在代码中使用大量静态字符串。将它们作为可以轻松本地化的 plist 或文本文件放入应用程序包装器中。

(并且,正如评论者所建议的那样,不要将您的应用程序的内容放入问题中——有些人可能会觉得它令人反感)

于 2012-07-20T06:29:55.090 回答
-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程序语法书。

于 2012-07-20T07:21:30.863 回答