使用 Objective C 解析文本文件(containsString 问题:)
Parsing text file with Objective C (issues with containsString:)
我试图从文件中读取的文本如下所示:"DREAM_TITLE: blah blah blah"。我 运行 遇到的问题是在 for 循环(特别是 containsString 方法)中,它一直告诉我密钥 DREAM_TITLE 不存在,而当它显然存在并且它甚至被加载时初始数组。请暂停!这里非常菜鸟,抱歉,如果有人被冒犯了。谢谢!
-(NSMutableArray *)findValueForKey:(NSString *)key{
NSString *path = [[NSBundle mainBundle] pathForResource:@"sampleData"
ofType:@"txt"];
NSString *fileContent = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:NULL];
NSMutableArray *arrayOfKeyValues = [[NSMutableArray alloc] init];
NSArray *numberOfLines = [[NSArray alloc] initWithObjects:[fileContent componentsSeparatedByString:@"\n"], nil];
for (int i=0; i<[numberOfLines count]; i++){
if ([[numberOfLines objectAtIndex:i] containsObject:key]){
NSArray *tempArray = [numberOfLines[i] componentsSeparatedByString:@":"];
[arrayOfKeyValues insertObject:[tempArray objectAtIndex:1] atIndex:i];
}
else {
[arrayOfKeyValues insertObject:@"no value for key found" atIndex:i];
}
}
return arrayOfKeyValues;
您出于某种原因创建了一个数组的数组,而原始数组就足够了:
NSArray *numberOfLines = [[NSArray alloc] initWithObjects:[fileContent componentsSeparatedByString:@"\n"], nil];
这可以很简单:
NSArray *lines = [fileContent componentsSeparatedByString:@"\n"];
接下来,要搜索数组中的每一行,只需使用 [NSString rangeOfString:]
:
for (NSUInteger i = 0; i < [lines count]; i++) {
if ([lines[i] rangeOfString:key].location != NSNotFound) {
NSArray *tempArray = [lines[i] componentsSeparatedByString:@":"];
[arrayOfKeyValues addObject:tempArray[1]];
} else {
[arrayOfKeyValues addObject:@"no value for key found"];
}
}
尽管这不是特别准确,因为您应该只在 :
...
之前的文本中搜索 key
我试图从文件中读取的文本如下所示:"DREAM_TITLE: blah blah blah"。我 运行 遇到的问题是在 for 循环(特别是 containsString 方法)中,它一直告诉我密钥 DREAM_TITLE 不存在,而当它显然存在并且它甚至被加载时初始数组。请暂停!这里非常菜鸟,抱歉,如果有人被冒犯了。谢谢!
-(NSMutableArray *)findValueForKey:(NSString *)key{
NSString *path = [[NSBundle mainBundle] pathForResource:@"sampleData"
ofType:@"txt"];
NSString *fileContent = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:NULL];
NSMutableArray *arrayOfKeyValues = [[NSMutableArray alloc] init];
NSArray *numberOfLines = [[NSArray alloc] initWithObjects:[fileContent componentsSeparatedByString:@"\n"], nil];
for (int i=0; i<[numberOfLines count]; i++){
if ([[numberOfLines objectAtIndex:i] containsObject:key]){
NSArray *tempArray = [numberOfLines[i] componentsSeparatedByString:@":"];
[arrayOfKeyValues insertObject:[tempArray objectAtIndex:1] atIndex:i];
}
else {
[arrayOfKeyValues insertObject:@"no value for key found" atIndex:i];
}
}
return arrayOfKeyValues;
您出于某种原因创建了一个数组的数组,而原始数组就足够了:
NSArray *numberOfLines = [[NSArray alloc] initWithObjects:[fileContent componentsSeparatedByString:@"\n"], nil];
这可以很简单:
NSArray *lines = [fileContent componentsSeparatedByString:@"\n"];
接下来,要搜索数组中的每一行,只需使用 [NSString rangeOfString:]
:
for (NSUInteger i = 0; i < [lines count]; i++) {
if ([lines[i] rangeOfString:key].location != NSNotFound) {
NSArray *tempArray = [lines[i] componentsSeparatedByString:@":"];
[arrayOfKeyValues addObject:tempArray[1]];
} else {
[arrayOfKeyValues addObject:@"no value for key found"];
}
}
尽管这不是特别准确,因为您应该只在 :
...
key