プロジェクトにweekdays.txtファイルがあります。このファイルには、スペースが含まれる曜日があります。そして、私のコードは機能していません。
NSString* path = [[NSBundle mainBundle] pathForResource:@"weekdays" ofType:@"txt"];
weekdaysDataSource = [[NSArray alloc] initWithContentsOfFile:path];
問題はどこにありますか?ありがとう。
プロジェクトにweekdays.txtファイルがあります。このファイルには、スペースが含まれる曜日があります。そして、私のコードは機能していません。
NSString* path = [[NSBundle mainBundle] pathForResource:@"weekdays" ofType:@"txt"];
weekdaysDataSource = [[NSArray alloc] initWithContentsOfFile:path];
問題はどこにありますか?ありがとう。
あなたのファイルはどのような形式ですか?
のドキュメントは-initWithContentsOfFile、path「writeToFile:atomically: メソッドによって生成された配列の文字列表現を含むファイルへのパス」である必要があります。それはプロパティリストになります。
ファイルがプロパティ リストでない場合は、ファイルを NSString ( +[NSString stringWithContentsOfFile:encoding:error:]) に読み込んでから、単語 ( ) に分割できます-[NSArray componentsSeparatedByString:]。
次のこともできます。
// 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 は、これが機能することを証明するために、ファイル内に入力したものを実際に出力します。