【问题标题】:createFileAtPath & OCUnit & errno bluescreateFileAtPath & OCUnit & errno blues
【发布时间】:2013-03-22 19:14:26
【问题描述】:

我查看了herethere,但无济于事。

考虑:

- (void) write: (NSString *) xId data:(NSData *) data forClass: (Class) c {
    NSFileManager * fm = [NSFileManager defaultManager] ;

    NSString * instancePath = [self instancePath:xId forClass: c] ;

    errno = 0 ;
    BOOL success = [fm createFileAtPath: instancePath
                               contents: data
                             attributes: nil] ;
    if (!success) {
        ::NSLog(@"Couldn't write to path: %@", instancePath) ;
        ::NSLog(@"Error was code: %d - message: %s", errno, strerror(errno));
    } else {
        ::NSLog(@"COULD write to path: %@", instancePath) ;
        ::NSLog(@"Error was code: %d - message: %s", errno, strerror(errno));
    }
}

然后打印:

2013-03-22 18:59:27.177 otest[18490:303] COULD write to path: /Users/verec/Library/Application Support/iPhone Simulator/6.1/Documents/cal/ModelRepo/ModelRepo#0.sexp
2013-03-22 18:59:27.177 otest[18490:303] Error was code: 3 - message: No such process
2013-03-22 18:59:27.178 otest[18490:303] Couldn't write to path: /Users/verec/Library/Application Support/iPhone Simulator/6.1/Documents/cal/ModelContainer/20130322.sexp
2013-03-22 18:59:27.178 otest[18490:303] Error was code: 3 - message: No such process
  1. 为什么 errno 不为 0,即使在第一种情况下 `success' 为 YES(“可能”情况)
  2. 任何人都可以发现实际路径中的区别,即在第一种情况下(“COULD”)使success=YES,而在第二种情况下(“Couldn't”),success=NO?

这是在运行 OCUnit 测试时,Xcode 4.6.1 运行 iOS 6.1 的模拟器

我只是感到困惑:-(

【问题讨论】:

    标签: objective-c nsfilemanager


    【解决方案1】:
    • errno 变量通常仅在调用失败时由系统调用(和一些库函数)设置。如果系统调用成功,它不会被修改,并且可能包含来自先前错误的非零值。

    • 应该在系统调用失败后立即打印或保存errno。在你的情况下,

      NSLog(@"Couldn't write to path: %@", instancePath);
      

      实际上修改了errno。 (“没有这样的过程”不太可能是正确的失败原因。)

    • 出于同样的原因,您不能假设errnocreateFileAtPath 失败后包含正确的值。在我的测试中确实如此,但它 没有记录该方法正确设置/保留errno

    【讨论】:

    • 接受导致问题的答案。出于某种原因,createFileAtPath 会或不会无缘无故地创建中间目录。修复方法是强制使用 createDirectoryAtPath:withIntermediateDirectories:attributes:error: 创建中间目录
    【解决方案2】:

    我写这个答案是为了用代码块更详细地解决 verec 的错误代码。实际上,他在对已接受答案的评论中提到了这一点。

    他从createFileAtPath得到的错误是3 (ESRCH) - No Such Process

    可能出现这种情况的一个原因是createFileAtPath 不会创建中间目录,因此如果指向您要创建的路径的目录不存在,它将失败并显示此错误代码。

    您必须先使用createDirectoryAtPath:withIntermediateDirectories:attributes:error: 创建目录,然后在成功创建目录后使用createFileAtPath

    NSString *fileParentFolderPath;
    NSString *filePath;
    
    //first create the directory, createFileAtPath can't create intermediate dirs
    NSError *error;
    if([[NSFileManager defaultManager] createDirectoryAtPath:fileParentFolderPath
            withIntermediateDirectories:YES attributes:nil error:&error]) {
        //then create the file
        if(![[NSFileManager defaultManager] createFileAtPath:filePath 
                contents:nil attributes:nil]) {
            NSLog(@"Faliure creating File error was code: %d - message: %s", errno, strerror(errno));
        };
    } else {
        NSLog(@"Faliure creating dir w error: %@", error);
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-05
      • 1970-01-01
      • 2022-10-17
      • 1970-01-01
      • 2011-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多