objective-c - NSString 到 FSRef 转换不起作用

标签 objective-c cocoa nsstring macos-carbon

对于我的应用程序,我需要使用 Carbon 文件管理器 API 来获取文件夹的大小(NSEnumerator 很慢,并且使用带有 shell 命令的 NSTask 更糟糕)。我已经导入了Carbon框架,并且使用这个方法来获取文件夹的大小:

http://www.cocoabuilder.com/archive/message/cocoa/2005/5/20/136503

它使用 FSRef 作为参数,而我的路径字符串当前是 NSString。我尝试使用它将 NSString 转换为 FSRef:

FSRef f;
        OSStatus os_status = FSPathMakeRef((const UInt8 *)[filePath fileSystemRepresentation], &f, NULL);

        if (os_status != noErr) {
            NSLog(@"fsref creation failed");
        }

然后我调用了文件夹大小方法:

[self fastFolderSizeAtFSRef:f];

但是,当我尝试构建时,我收到有关上述行的错误:

错误:“fastFolderSizeAtFSRef:”参数之一的类型不兼容

如有任何帮助,我们将不胜感激。谢谢

最佳答案

“fastFolderSizeAtFSRef”方法采用 FSRef*(FSRef 指针)。你给它一个 FSRef。幸运的是,这是一个单一角色的修复。你有:

[self fastFolderSizeAtFSRef:f];

只需将其更改为:

[self fastFolderSizeAtFSRef:&f];

你应该可以开始了。然而,我昨天实现了同样的方法,但在创建 FSRef 本身时遇到了麻烦。我最终使用了以下代码:

FSRef convertNSStringToFSRef(NSString * theString) {
    FSRef output;
    const char *filePathAsCString = [theString UTF8String];
    CFURLRef url = CFURLCreateWithBytes(kCFAllocatorDefault, 
                                        (const UInt8 *)filePathAsCString, 
                                        strlen(filePathAsCString),
                                        kCFStringEncodingUTF8,
                                        NULL);
    CFURLGetFSRef(url, &output);
    CFRelease(url);
    return output;
}

这对我来说非常完美。

编辑:我刚刚开源了我正在使用的代码。这些文件是:

This file adds a method to NSFileManager that uses an FSRef to find the complete size of a directoryThis file adds a method to NSString that will convert it to an FSRef .
Everything happens on line 46 of this file .

关于objective-c - NSString 到 FSRef 转换不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1280799/

相关文章:

iphone - 在 Objective-C 中将单个字符串分成两个不同的字符串

iOS StoreKit - 何时调用 - (void)restoreCompletedTransactions?

ios - 从 UIImage 更改 ViewController

macos - NSSpeechSynthesizer 语音/语言关系

cocoa - 如何向 NSSplitter handle 添加按钮?

ios - 使用 NSDateFormatter 从 NSString 到 NSDate 转换的预期输出偏差

c++ - Objective-C 中的非规范化 float ?

ios - 具有 '-' 字符的字符串在 iOS 的 drawrect 方法中未正确绘制

objective-c - SFV 文件的 ComponentsSeparatedByString

arrays - 将字符串拆分为数组的最佳方法