2012-04-27 22 views
1

私はアプリケーションが初期化された後にファイルを追加したいアプリケーションの中にプリロードされたフォルダを持っているので、NSApplicationSupportDirectoryにファイルを保存しようとしています。私はNSLogファイルの内容をしようとしているので、実際に働いているかどうかを確認することができます。デバッガから、私はコンテンツがあることを知ることができます。これは私が意味するものではありません。誰ですか?NSApplicationSupportDirectoryが無効です。CFStringRef

NSString *document = [NSString stringWithFormat:@"%@ %@ %@ %@ %@", description, focus, level, equipment, waterDepth]; 

//NSLog(@"%@", document); 

//get the documents directory: 
NSArray *paths = NSSearchPathForDirectoriesInDomains 
(NSApplicationSupportDirectory, NSUserDomainMask, YES); 
NSString *supportDirectory = [paths objectAtIndex:0]; 

//filename 
NSString *filename = [NSString stringWithFormat:@"%@/%@", supportDirectory,[_nameTextField.text stringByAppendingString:@".txt"]]; 
NSLog(@"%@", supportDirectory); 
NSLog(@"%@", filename); 

[document writeToFile:filename atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil]; 

NSString *content = [[NSString alloc]initWithContentsOfFile:filename usedEncoding:nil error:nil]; 
NSLog(@"%@", content); 
+0

名:の/ var /モバイル/アプリケーション/ 5461D615-8F57-4BBB-AEB8-482B4BF3BA36 /ライブラリ/ Application Support/test.txtの –

+0

コンテンツ:(ヌル)エラーのNSLogで –

+0

アプリケーションがクラッシュ:EXC_BAD_ACCESS(コード= 1 、adderss = 0x104) –

答えて

1

ファイルを書いたり読んだとき、非常にエラーパラメータを使用して、エラーを取得するケースを処理することをお勧めします。また、これはコードをデバッグするのに役立ちます。あなたはThe operation couldn’t be completed. No such file or directoryと言うエラーが出た場合、それはあなたが以前にフォルダを作成していないことを意味し

NSError *error = nil; 
BOOL success = [document writeToFile:filename atomically:NO encoding:NSStringEncodingConversionAllowLossy error:&error]; 

if (!success) { 
    NSLog(@"Could not write file: %@", error); 
} else { 
    NSString *content = [[NSString alloc]initWithContentsOfFile:filename usedEncoding:nil error:&error]; 
    if (!content) { 
     NSLog(@"Could not read file: %@", error); 
    } 
} 

:たとえば

は、あなたのケースでは、あなたは次のように行うことができます。だから、あなたはそれにコンテンツを追加しようとする前にそれを作成します。

NSString *supportDirectory = [paths objectAtIndex:0]; 
NSError *error = nil; 
BOOL success; 
if (![[NSFileManager defaultManager] fileExistsAtPath: supportDirectory]) { 
    success = [[NSFileManager defaultManager] createDirectoryAtPath:supportDirectory withIntermediateDirectories:YES attributes:nil error:&error]; 
    if (!success) { 
     NSLog(@"Could not create directory: %@", error); 
    } 
} 
+0

デバッガでは、パスにファイルを書き込めないと言っていますか? –

+0

パスにファイルを書き込めませんでした:Error Domain = NSCocoaErrorDomain Code = 517 "操作を完了できませんでした(Cocoaエラー517)。" UserInfo = 0xd6dbae0 {NSFilePath =/var/mobile/Applications/80167553-DF41-49D6- –

+0

私はまだよく分かりませんが、ファイルがドキュメントフォルダに保存されているかどうかを確認するために作業しています。あなたの助けをありがとう、私は正しい方向に私を導く:) –

9

をあなたは、文字列のエンコーディングを渡していない、あなたは、エンコーディング変換は特定のメソッドのために起こる方法に影響を与えることができますオプション(ではないですNSStringEncodingConversionAllowLossyを渡していますあなたが使用しています)。 NSUTF8StringEncodingのようなものを渡す必要があります。

+0

これは、OPsの問題に対する真の解決策です。彼は次のエラーが発生していると言いました。パスでファイルを書き込めませんでした:Error Domain = NSCocoaErrorDomain Code = 517 "操作を完了できませんでした(Cocoaエラー517)。 "ユーザー情報= 0xd6dbae0 {NSFilePath =/var/mobile/Applications/80167553-DF41-49D6-BA1C-91D5EA6590FA/Librar y/Application Support/test.txt、NSStringEncoding = 1} - –

+0

私も同じ問題に直面していました。 NSUTF8StringEncodingのように渡すことによって解決されます。ありがとうKen –

+0

それも私を働いた。 –

関連する問題