2010-07-23 29 views
2

我想從文本文件創建一個字符串的NSArray。從文本文件創建對象的NSArray

這是我一直在使用的代碼:

但它似乎給了一些額外的對象在,我需要解決這個沒有文字。 只是如果它幫助我一直在使用它了這個文本文件:

/NFEET在英格蘭尺寸測量Barleycorns 。/n

大象笑和哭泣。/n

褲子一年有12人死亡。/n

MGM標誌中咆哮的獅子叫做Volney,它的號碼是 。/n

蘇丹的妻子被稱爲 蘇丹娜。/n

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

有5000多個瓢蟲 種。/n

美國洗手間比英國洗手間多100倍 危險。在1996年, 43,687美國人在廁所災難後住院 。/n

自由女神像是由 青銅製成,並被法國人送給美國人 。/n

愛爾蘭作爲一個笑話給蘇格蘭人的風笛 。它回來了!/n

你的腳與手腕和手肘之間的距離相同,長度爲 。 /n

老鼠可以不停地游泳72小時。 /N

的英國汽車 崩潰被殺害的機率是相同的是 在自己家裏 內的事故中喪生:1萬。/n

1996年,英國12人分別被 送回醫院,事件發生在回形針 之後。/n

+1

以防萬一,換行符爲\ n,而不是/ N ...但也許你已經寫/ N在您的文件... – Macmade 2010-07-23 16:46:58

回答

4

這裏是文本文件:

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. 

下面是代碼:

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線輸入f中的文本你指定的。在文件的開頭有一個額外的換行符。這說明了一個額外的計數。我只能推測在實際文件中有一個額外的新行。也許到最後?

有一些事情我做了這項工作。 我改變@"/n"@"\n" 我們通過nilerror:NULL 我記錄的NSMutableArray裏,我選擇了在可讀性第二步初始化的說明。 我也擺脫了文件頂部的額外換行符,並確保最後沒有一個換行符。

1

如何將數組記錄到控制檯(或使用「po factArray」打印)?額外的對象可能是空行,也許?如果是這樣,你可以刪除isEqualToString的所有對象(字符串):@「」...

+0

當我登錄使用'爲陣列(的NSString *文本self.facts) ''{''NSLog''(@「%@」,''text);''}''只顯示14個項目,當計數顯示爲16時。 – Joshua 2010-07-23 18:50:02

+0

發佈。追加輸出到你的問題,所以我們可以看到你看到的。 – 2010-07-23 19:22:21

+0

這是我看到的。 http://cl.ly/5f6757054624b3406fb6 – Joshua 2010-07-24 09:38:45