从数组中获取特定长度的字符串

发布于 2024-11-26 05:53:10 字数 714 浏览 2 评论 0原文

我有这段代码可以在文本文件中读取,单词之间用换行符分隔。我想要做的是将所有单词读入一个数组,然后从该数组中选取所有六个字母的单词。

我有下面的代码,但它似乎在 for 循环内生成错误。

另外,读入文本文件后,我是否必须释放它?

NSString* path = [[NSBundle mainBundle] pathForResource:@"newdict" ofType:@"txt"];

NSString* content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];

NSArray* allLinedStrings = [content componentsSeparatedByCharactersInSet:
[NSCharacterSet newlineCharacterSet]];

int wordcount = [allLinedStrings count];
int i;
NSMutableArray* sixLetterWords;

for( i = 0 ; i < wordcount ; i++)
{
    NSString* word = [allLinedStrings objectAtIndex: i];
    if (StrLength(word) == 6)
        [sixLetterWords addObject:word];
}

I have this code to read in a text file, with words separated by newlines. What I want to do is read all the words into an array and then pick all the six-letter words from that array.

I have this code below, but it seems to be generating an error from within the for loop.

Also, after reading in the text file, do I have to release it?

NSString* path = [[NSBundle mainBundle] pathForResource:@"newdict" ofType:@"txt"];

NSString* content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];

NSArray* allLinedStrings = [content componentsSeparatedByCharactersInSet:
[NSCharacterSet newlineCharacterSet]];

int wordcount = [allLinedStrings count];
int i;
NSMutableArray* sixLetterWords;

for( i = 0 ; i < wordcount ; i++)
{
    NSString* word = [allLinedStrings objectAtIndex: i];
    if (StrLength(word) == 6)
        [sixLetterWords addObject:word];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

朕就是辣么酷 2024-12-03 05:53:10

比 for 循环更好的选择是 快速枚举

// Don't forget to actually create the mutable array
NSMutableArray * sixLetterWords = [[NSMutableArray alloc] init];
for( NSString * word in allLinedStrings ){
    if( [word length] == 6 ) [sixLetterWords addObject:word];
}    

块基于枚举enumerateObjectsUsingBlock:

NSMutableArray * sixLetterWords = [[NSMutableArray alloc] init];
[allLinedStrings enumerateObjectsUsingBlock:^(id word, NSUInteger idx, BOOL * stop){
    if( [(NSString *)word length] == 6 ) [sixLetterWords addObject:word];
 }];

也有可能 过滤数组

NSArray * sixLetterWords = [allLinedStrings filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"length == 6"

请注意,最后一个选项为您提供了一个自动释放的数组——如果您想保留它,则必须保留它。有了这些,您就不再需要担心数组长度或显式索引;它由数组为您处理。 快速枚举也比普通的for循环更快

您用于将文本文件读入字符串的方法 stringWithContentsOfFile:encoding:error: 不是 newalloc,也不是它以 copymutableCopy 开头;因此,根据 Cocoa内存管理规则,你不拥有它,也不必释放它。 (如果您希望它保留在当前方法的末尾之后,则需要保留它。)

Better options than a for loop are fast enumeration:

// Don't forget to actually create the mutable array
NSMutableArray * sixLetterWords = [[NSMutableArray alloc] init];
for( NSString * word in allLinedStrings ){
    if( [word length] == 6 ) [sixLetterWords addObject:word];
}    

and blocks-based enumeration with enumerateObjectsUsingBlock::

NSMutableArray * sixLetterWords = [[NSMutableArray alloc] init];
[allLinedStrings enumerateObjectsUsingBlock:^(id word, NSUInteger idx, BOOL * stop){
    if( [(NSString *)word length] == 6 ) [sixLetterWords addObject:word];
 }];

There is also the possibility to filter the array:

NSArray * sixLetterWords = [allLinedStrings filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"length == 6"

Note that this last option gives you an autoreleased array -- if you want to keep it around, you must retain it. With any of these, you no longer have to worry about the array length or explicit indexing; it is handled for you by the array. Fast enumeration is also, as its name indicates, faster than a plain for loop.

The method that you used to read the text file into your string, stringWithContentsOfFile:encoding:error:, is not new or alloc, nor does it begin with copy or mutableCopy; therefore, according to Cocoa memory management rules, you do not own it and do not have to release it. (And if you want it to stick around past the end of the current method, you will need to retain it.)

趴在窗边数星星i 2024-12-03 05:53:10

您不需要释放文本文件,因为它将自动释放。

编辑:

你需要分配并初始化你的 NsMutableArray ...

NSMutableArray* sixLetterWords = [[NSMutableArray alloc] init];

我的 for 循环有点错误,你第一次就做对了。

You do not need to release your text file since it will be autoreleased.

EDIT:

You need to alloc and init you NsMutableArray...

NSMutableArray* sixLetterWords = [[NSMutableArray alloc] init];

I had the for loop bit wrong, you had it right the first time.

杀お生予夺 2024-12-03 05:53:10

不想自吹自擂, CMFunctionalAdditions 框架可以更干净、更并发地完成此操作:)

NSArray* sixLetterWords = [allLinedStrings filterWithPredicate:^BOOL(NSString* str) {
    return [str length] == 6;
}];

Without wanting to blow my own trumpet, the CMFunctionalAdditions framework can do this more cleanly and concurrently :)

NSArray* sixLetterWords = [allLinedStrings filterWithPredicate:^BOOL(NSString* str) {
    return [str length] == 6;
}];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文