Cocoa touch 零输出故障
好吧,我已经试了2-3个小时了,但我似乎不太明白。以下是代码和简要说明: 我尝试制作两个单词列表,从每个列表中随机抽取一个单词,当按下按钮时,在屏幕上显示两个单词(以及第三个单词)。代码如下:Cocoa touch 零输出故障,cocoa-touch,null,Cocoa Touch,Null,好吧,我已经试了2-3个小时了,但我似乎不太明白。以下是代码和简要说明: 我尝试制作两个单词列表,从每个列表中随机抽取一个单词,当按下按钮时,在屏幕上显示两个单词(以及第三个单词)。代码如下: #import "Project001ViewController.h" @implementation Project001ViewController -(ArrayOfWords *) advs { if(!advs){ ad
#import "Project001ViewController.h"
@implementation Project001ViewController
-(ArrayOfWords *) advs
{
if(!advs){
advs = [[ArrayOfWords alloc] init];
NSString* advpath = @"/WordLists/adverbs.txt";
NSLog(@"1");
[[self advs] populateListOfWords:advpath];
}
return advs;
}
-(ArrayOfWords *) adjs
{
if (!adjs) {
adjs = [[ArrayOfWords alloc] init];
NSString* adjpath = @"/WordLists/adjectives.txt";
[[self adjs] populateListOfWords:adjpath];
NSLog(@"2");
}
return adjs;
}
- (IBAction)generate:(UIButton *)sender;
{
//int randy = arc4random() % 11;
//NSNumber* num= [NSNumber numberWithInteger:randy];
NSString* obj = @"app";
NSString* adverb = [[self advs] randomItem];
NSString* adjective = [[self adjs] randomItem];
NSLog(@"%i %i",[adjs size],[advs size]);
NSLog(@"1 %@ %@ %@.",adverb, adjective, obj);
//NSLog(@"%@",thePhrase);
[display setText:@"Hi"];
}
@end
我在最后的NSLog
行遇到问题:
NSString* obj = @"app";
NSString* adverb = [[self advs] randomItem];
NSString* adjective = [[self adjs] randomItem];
NSLog(@"%i %i",[adjs size],[advs size]);
NSLog(@"1 %@ %@ %@.",adverb, adjective, obj);
数组不获取两个随机选择的单词(使用arc4random()
生成它们),而是返回Null
。但我可以肯定。数组不是空的,因为我打印[adjs size]
和[advs size]
的NSLog
行得到了单词列表的正确大小。我只想知道是什么导致他们在这里打印Null
PopulateListofWord、randomItem和大小方法:
- (NSArray *) populateListOfWords:(NSString *) path {
//gets the components of the file into an NSString
NSString *wordListString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
//returns an array of all the words (uses the next line indicator '\n' to know when it's at the end of the word
NSArray* words = [wordListString componentsSeparatedByString:@"\n"];
length=(NSNumber*)([words count]);
return words;
}
-(NSString*) randomItem{
//returns random object in list
return (NSString*)[list objectAtIndex:(arc4random() % (int)length)] ;
}
-(int) size{
//returns size of list
return (int)length;
}
(如果需要更多代码,请让我知道,并在advanced中感谢您提供的所有帮助)。我认为路径有问题。由于应用程序沙箱的原因,无法在iOS中访问路径
/wordlist/approxes.txt
。我建议您将这些文件拖放到项目中,将其添加到应用程序中。您可以使用获取应用程序包中资源的文件路径
NSString * path = [[NSBundle mainBundle] pathForResource:@"adjectives" ofType:@"txt"];
现在将此路径
传递给方法populateListOfWords:
由于路径不正确,我认为wordListString
是nil
,接下来的所有内容都是这样
<> P>另一个问题是<代码> int <代码>和<代码> NStudio不是免费的桥接,如<代码> NScord S和其他基础对象。所以
length=(NSNumber*)([words count]);
这是不正确的。我建议您将
length
定义为int
或更好的NSUInteger
,以匹配count
方法返回的类型。此方法存在问题:
- (NSArray *) populateListOfWords:(NSString *) path {
//gets the components of the file into an NSString
NSString *wordListString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
//returns an array of all the words (uses the next line indicator '\n' to know when it's at the end of the word
NSArray* words = [wordListString componentsSeparatedByString:@"\n"];
length=(NSNumber*)([words count]);
return words;
}
实际上,它并没有把这些词放在任何其他人都可以访问的列表中。我不得不这样修改它:
- (void) populateListOfWords:(NSString *) path {
//gets the components of the file into an NSString
NSString *wordListString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
//returns an array of all the words (uses the next line indicator '\n' to know when it's at the end of the word
NSArray* words = [wordListString componentsSeparatedByString:@"\n"];
list = words;
length=(int)([words count]);
}
现在它给了我正确的输出。但由于某种原因,当我按两次按钮时,它崩溃了。哦,这是一个新问题。再次感谢你的帮助
更新
原来
advs
和adjs
正在被释放,所以第二次循环尝试访问一个nil值,因为当我调用[self advs][self adjs]
时,指针存在,但其内容不存在。我每次都得回去重新装,基本上都是把if(!advs)
和if(adjs)
零件去掉。现在它可以按预期工作。如何实现randomItem
,size
方法是否返回包含单词的数组的计数?当我填充列表时,它将由文本文件中的NSString填充<代码>-(NSString*)随机项{//返回列表返回中的随机对象(NSString*)[列表对象索引:(arc4random()%(int)length)];}-(int)size{//返回列表返回的大小(int)length;}我认为还值得注意的是,我使用了objectAtIndex
调用尝试从数组中获取一些内容,它还返回Null
我想知道这是否是一个问题。我应该把它们放在哪里?我试过了,结果还是一样的。在NSLog中,它仍然显示“1(null)(null)app”。尽管如此,由于计数的输出是正确的,它还是在填充列表。