Objective c 从文本文件创建对象的NSArray

Objective c 从文本文件创建对象的NSArray,objective-c,cocoa,Objective C,Cocoa,我正在尝试从文本文件创建字符串的NSArray 以下是我一直使用的代码: NSString *title = @"facts"; NSString *type = @"txt"; NSString *seperation = @"/n"; NSMutableArray *factArray = [[NSArray alloc] initWithArray:[[NSString stringWithContentsOfFile:[[NSBundle mainBundle]

我正在尝试从文本文件创建字符串的NSArray

以下是我一直使用的代码:

NSString *title = @"facts";
    NSString *type = @"txt";
    NSString *seperation = @"/n";
    NSMutableArray *factArray = [[NSArray alloc] initWithArray:[[NSString stringWithContentsOfFile:[[NSBundle mainBundle] 
                                                            pathForResource:title ofType:type] 
                                                                    encoding:NSMacOSRomanStringEncoding error:NULL] 
                                                         componentsSeparatedByString:seperation]];
但它似乎给了一些没有文本的额外对象,我需要解决这个问题。 如果它有助于我使用它的文本文件:

/英国的nFeet尺寸是测量的 在Barleycorns/n

大象又哭又笑/n

裤子每年杀死12人/n

米高梅标志中咆哮的狮子是 叫沃尼/n

苏丹的妻子被称为国王 苏丹娜/n

布拉德·皮特的真名是威廉/n

有5000多只瓢虫 物种/n

美国厕所的价格是美国的100倍 比英国的更危险。1996年,, 43687名美国人住院治疗 厕所灾难之后/n

自由女神像是由玻璃制成的 铜牌和铜牌被授予了美国人 法国的/n

爱尔兰人把风笛送给了苏格兰人 开个玩笑。它反击了/n

你的脚和你的脚一样长 手腕和肘部之间的距离。 /n

老鼠可以不停地游泳72小时。 /n

在车里被杀的几率 在英国发生的撞车事故与在英国发生的撞车事故是一样的 在你自己家里的一次事故中丧生 家庭:万分之一/n

1996年,英国共有12人失业 一枚回形针后被送往医院 事件/n


将阵列记录到控制台(或使用“po factArray”打印)如何?额外的对象可能是空行,也许?如果是这样,您只需删除所有IsequalString:@“”…

的对象(字符串)以下是文本文件:

代码如下:

NSString *title = @"facts";
NSString *type = @"txt";
NSString *separation = @"\n";
NSString *fileText = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:title ofType:type] encoding:NSMacOSRomanStringEncoding error:nil];
NSMutableArray *facts = [[NSMutableArray alloc]initWithArray:[fileText componentsSeparatedByString:separation]];

NSLog(@"%@", [facts description]);
以下是输出数组:

(
"Feet sizes in England are measured in Barleycorns. ",
"Elephants laugh and cry. ",
"Trousers kill 12 people a year. ",
"The lion that roars in the MGM logo is called Volney. ",
"The wife of a Sultan is called a Sultana. ",
"Brad Pitt's real name is William. ",
"There are more than 5000 ladybird species. ",
"US lavatories are 100 times more dangerous than British ones. In 1996, 43,687 Americans were hospitalized after toilet disasters. ",
"The statue of liberty is made of bronze and was given to the Americans by the French. ",
"The Irish gave bagpipes to the Scots as a joke. It back fired! ",
"Your foot is the same length as the distance between your wrist and elbow. ",
"A rat can swim non-stop for 72 hours. ",
"The odds of being killed in a car crash in Britain are the same as being killed in an accident inside your own home: 1 in 10,000. ",
"In 1996, 12 people in Britain were rushed to hospital after a paperclip incident. "
)
您指定的输入文件中有14行文本。文件开头有一个额外的换行符。这是额外计算的一个原因。我只能推测,在实际文件的某个地方有一个额外的新行。也许在最后

我做了几件事来让这一切顺利进行。 我将
@”/n“
更改为
@”\n“
我们将
nil
传递给
错误:
not
NULL
我记录了NSMutableArray的描述,为了可读性,我选择在第二步中初始化它。
我还去掉了文件顶部多余的换行符,并确保末尾没有换行符。

以防万一,换行符是\n,而不是/n。。。但可能您已经在文件中写入了/n…当我使用`for(self.facts中的NSString*text)
{
NSLog
(@“%@,
text);```}`记录数组时,当计数显示为16时,它只显示14项。发布它。将输出附加到您的问题,以便我们可以看到您看到的内容。
(
"Feet sizes in England are measured in Barleycorns. ",
"Elephants laugh and cry. ",
"Trousers kill 12 people a year. ",
"The lion that roars in the MGM logo is called Volney. ",
"The wife of a Sultan is called a Sultana. ",
"Brad Pitt's real name is William. ",
"There are more than 5000 ladybird species. ",
"US lavatories are 100 times more dangerous than British ones. In 1996, 43,687 Americans were hospitalized after toilet disasters. ",
"The statue of liberty is made of bronze and was given to the Americans by the French. ",
"The Irish gave bagpipes to the Scots as a joke. It back fired! ",
"Your foot is the same length as the distance between your wrist and elbow. ",
"A rat can swim non-stop for 72 hours. ",
"The odds of being killed in a car crash in Britain are the same as being killed in an accident inside your own home: 1 in 10,000. ",
"In 1996, 12 people in Britain were rushed to hospital after a paperclip incident. "
)