NSString 到 FSRef 转换不起作用

发布于 2024-08-01 21:15:58 字数 844 浏览 5 评论 0原文

对于我的应用程序,我需要使用 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];

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

error: incomplete type for argument one of 'fastFolderSizeAtFSRef:'

Any帮助将不胜感激。 谢谢

For my app I need to use the Carbon file manager API to get the size of a folder (NSEnumerator is slow, and using NSTask with a shell command is even worse). I've imported the Carbon framework, and I'm using this method to get the size of a folder:

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

It uses an FSRef as an argument, and my path string is currently an NSString. I tried using this to convert the NSString to an FSRef:

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

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

And then I called the folder size method:

[self fastFolderSizeAtFSRef:f];

However when I try to build, I get this error regarding the above line:

error: incompatible type for argument one of 'fastFolderSizeAtFSRef:'

Any help would be appreciated. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趴在窗边数星星i 2024-08-08 21:15:58

“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;
}

这对我来说工作完美。

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

此文件向 NSFileManager 添加了一个方法,该方法使用 FSRef 来查找目录的完整大小
此文件向 NSString 添加了一个方法,将其转换为 FSRef.
一切都发生在该文件的第 46 行

The "fastFolderSizeAtFSRef" method takes an FSRef* (FSRef pointer). You're giving it an FSRef. It's a one character fix, luckily enough. You have:

[self fastFolderSizeAtFSRef:f];

Simply change that to:

[self fastFolderSizeAtFSRef:&f];

And you should be good to go. However, I was implementing this same method yesterday but was having trouble creating the FSRef itself. I eventually went with the following code:

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 has been working flawlessly for me.

EDIT: I just open sourced the code that I'm using this in. It's these files:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文