2012-04-26 12 views
0

私は読み取り可能なファイルのコピーを作成できない理由を理解しようとしています。 moveItemAtPath、copyItemAtPath、およびcreateFileAtPathを試しました。毎回、元のパスからデータを読み込むのはうまくいきますが、新しいファイルパスから読み込むと、0バイトのNSdataオブジェクトがゼロになります。copyItemAtPath、moveItemAtPath、およびcreateFileAtPathはすべて、読み取れないファイルを作成するようです。

NSData* tempData = [NSData dataWithContentsOfURL:tempSoundFile]; // reads just fine 

NSError* error; 
NSFileManager* fileMgr = [NSFileManager defaultManager]; 
NSString* docDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; 
NSString* uniqueFileName = [[NSString stringWithFormat:@"%@-%@", description.text, [NSDate date]] MD5]; 
NSString* newAudioFile = [NSString stringWithFormat:@"%@/%@.caf", docDir, uniqueFileName]; 

if (LOG) { NSLog(@"Copying %@ to %@", tempSoundFile.path, newAudioFile); } 

// Have tried either or both as well as moveItemAtPath with the same result 
[fileMgr createFileAtPath:newAudioFile contents:tempData attributes:nil]; 
//[fileMgr copyItemAtPath:tempSoundFile.path toPath:newAudioFile error:&error]; 

NSData* audioAfter = [NSData dataWithContentsOfURL:[NSURL URLWithString:newAudioFile]]; // nil data 

ログ出力:/var/mobile/Applications/19531C7F-F408-45B9-B417-09315BB15B49/Documents/8554temp.cafへの/ var /モバイル/アプリケーション/ 19531C7F-F408-45B9のコピー

-B417-09315BB15B49/Documents/933becb02783c8f9c715acbdca0e15ed.caf

私が間違っていることは誰にもありますか?おかげ

+1

さまざまなファイルマネージャメソッドの戻り値をチェックして成功を示していると仮定することは安全でしょうか( 'YES'を返します)? '[NSURL URLWithString:newAudioFile]'が期待した結果を出すことも確認しましたか? –

+0

'audioAfter'はnilか、長さ0の有効なNSDataインスタンスですか? – Caleb

答えて

1

変更し、あなたの最後の行に:NSURLとパスのURLがファイルを期待

SData* audioAfter = [NSData dataWithContentsOfFile:newAudioFile]; // NOT nil 

ので://先頭に。

+0

ありがとう!それはまさに正しいことです。 URLは自動的にファイルを生成しませんでした://想定した通りです。 – valheru

関連する問題