1

プロジェクトにweekdays.txtファイルがあります。このファイルには、スペースが含まれる曜日があります。そして、私のコードは機能していません。

NSString* path = [[NSBundle mainBundle] pathForResource:@"weekdays" ofType:@"txt"];
weekdaysDataSource = [[NSArray alloc] initWithContentsOfFile:path];

問題はどこにありますか?ありがとう。

4

2 に答える 2

4

あなたのファイルはどのような形式ですか?

のドキュメントは-initWithContentsOfFilepath「writeToFile:atomically: メソッドによって生成された配列の文字列表現を含むファイルへのパス」である必要があります。それはプロパティリストになります。

ファイルがプロパティ リストでない場合は、ファイルを NSString ( +[NSString stringWithContentsOfFile:encoding:error:]) に読み込んでから、単語 ( ) に分割できます-[NSArray componentsSeparatedByString:]

于 2012-07-04T00:46:09.170 に答える
0

次のこともできます。

// create an NSURL object that holds the path of the file you want to read

NSURL *fileURL = [NSURL fileURLWithPath:@"/Users/yourname/weekdays.txt"];  

//Create an NSMutaable string object and use the stringWithContentsOfURL: encoding: error: method to hold whatever might be you have in the text file. 
//Just pass the NSURL object you created in the previous step to as an argument like so

NSMutableString *text = [NSMutableString stringWithContentsOfURL:fileURL encoding:NSUTF8StringEncoding error:nil];

NSLog(@"The content of the file is: %@", text);

以上です。NSLog は、これが機能することを証明するために、ファイル内に入力したものを実際に出力します。

于 2012-07-04T03:02:54.807 に答える