开发者

Best way to populate this array using .txt file?

开发者 https://www.devze.com 2023-03-20 04:06 出处:网络
I\'m looking to make SEVERAL arrays populated with words. Eventually I want to be able to just pull a random word from the array and display it (I\'ve mastered that). What I\'m wondering is what is th

I'm looking to make SEVERAL arrays populated with words. Eventually I want to be able to just pull a random word from the array and display it (I've mastered that). What I'm wondering is what is the best way to populate this array. Should I just type in:

[NSArray arrayWithObjects:@"word1",@"word2",@"word3",@"word4",@"word5",nil]

or is there a better way where the words ar开发者_StackOverflow社区e stored in a .txt file and I can just have a loop add each word in the text file to the Array?

I'm looking at filling the arrays with 100's of words. Any and all help is appreciated :D.

UPDATE

After doing some research I found this here. It seems to be exactly what I wanted. The only thing is it gives me a warning

'stringWithContentsOfFile' is deprecated

I know the full NSString method is:

stringWithContentsOfFile:(NSString *) encoding:(NSStringEncoding) error:(NSError **)

But I don't know what to put for encoding (and I'm assuming I can just put 'nil' for NSError). Other than that it works like a charm. I might consider switching from paths to urls. Here is the code that I found:

- (NSArray *) readFile:(NSString *) path {


NSString *file = [NSString stringWithContentsOfFile:path];

NSMutableArray *dict = [[NSMutableArray alloc] init];
NSCharacterSet *cs = [NSCharacterSet newlineCharacterSet];
NSScanner *scanner = [NSScanner scannerWithString:file];

NSString *line;
while(![scanner isAtEnd]) {
    if([scanner scanUpToCharactersFromSet:cs intoString:&line]) {
        NSString *copy = [NSString stringWithString:line];
        [dict addObject:copy];

    }
}
NSArray *newArray = [[NSArray alloc] initWithArray:dict];

return newArray;
}


You can probably use NSUTF8StringEncoding for the encoding parameter (depending on how you created the file, but this is the most common).

Instead of using NSScanner, you can also simply split the string into lines with the componentsSeparatedByString: method. This reduces your method to just these two lines:

NSString *file = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
return [file componentsSeparatedByString:@"\n"];

Btw, you shouldn't name an array variable "dict", this would imply that it's an NS(Mutable)Dictionary.


The less code way to get in a list of words would be:

NSString *path = [[NSBundle mainBundle] pathForResource:@"Words" ofType:@"plist"];
    NSArray *words = [[NSArray alloc] initWithContentsOfFile:path];

Then you just have to create a plist that has all the words in it with the root object as an array.

In the method you have above the encoding you are looking for is NSUTF8StringEncoding and you actually should pass an NSError by reference, if something goes wrong the error could be useful:

NSError *anError = nil;
NSString *file = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&anError];
0

精彩评论

暂无评论...
验证码 换一张
取 消