c++ - FSPathMakeRef 和 FSGetCatalogInfo 的 Cocoa 等价物是什么?

标签 c++ objective-c macos cocoa macos-carbon

我有一个使用 Carbon 的 C++ 库代码库。我需要将它升级到 Cocoa,因为我收到了这些警告:

警告:“FSPathMakeRef”已弃用:首先在 OS X 10.8 中弃用 [-Wdeprecated-declarations] 警告:“FSGetCatalogInfo”已弃用:首先在 OS X 10.8 中弃用 [-Wdeprecated-declarations] 警告:“FSSetCatalogInfo”已弃用:首先在 OS X 10.8 中弃用 [-Wdeprecated-declarations]

这是我使用这些函数的地方:

#ifdef MAC_LIKE
    OSErr result;
    OSType fileType;
    FSCatalogInfo catalogInfo;
    FSRef ref;

    result = FSPathMakeRef(pathname, &ref, NULL);
    BailError(result);

    result = FSGetCatalogInfo(&ref, kFSCatInfoNodeFlags|kFSCatInfoFinderInfo, &catalogInfo,
                    NULL, NULL, NULL);
    if (result) {
        BailError(kNuErrFileStat);
    }

    /* Build the type and creator */

    fileType = 0x70000000;
    fileType |= (pRecord->recFileType & 0xFF) << 16;
    fileType |= (pRecord->recExtraType & 0xFFFF);

    /* Set the type and creator */

    ((FileInfo *) &catalogInfo.finderInfo)->fileType = fileType;
    ((FileInfo *) &catalogInfo.finderInfo)->fileCreator = 'pdos';
    result = FSSetCatalogInfo(&ref, kFSCatInfoFinderInfo, &catalogInfo);
    BailError(result);
#endif

我的问题是:

1) 我该如何改变:

 result = FSPathMakeRef(pathname, &ref, NULL);

不使用 FSPathMakeRef 函数?

2) 我该如何改变:

   result = FSGetCatalogInfo(&ref, kFSCatInfoNodeFlags|kFSCatInfoFinderInfo, &catalogInfo,
                    NULL, NULL, NULL);

不使用 FSGetCatalogInfo 函数?

3) 我该如何改变:

result = FSSetCatalogInfo(&ref, kFSCatInfoFinderInfo, &catalogInfo);

不使用 FSSetCatalogInfo 函数?

最佳答案

NSFileManager 具有获取和设置文件属性(如文件类型和创建者)的方法。

示例(为简洁起见省略了错误检查):

NSString *path = @"/path/to/your/file";

// Get file type and creator:
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error;
NSDictionary *attr = [fm attributesOfItemAtPath:path error:&error];
unsigned long type = [attr[NSFileHFSTypeCode] unsignedLongValue];
unsigned long creator = [attr[NSFileHFSCreatorCode] unsignedLongValue];

// Set a new type and creator:
type = 'ABCD';
creator = 'pdos';
attr = @{NSFileHFSTypeCode : @(type), NSFileHFSCreatorCode : @(creator)};
[fm setAttributes:attr ofItemAtPath:path error:&error];

关于c++ - FSPathMakeRef 和 FSGetCatalogInfo 的 Cocoa 等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23416343/

相关文章:

ios - Mac 上出现 "No safari Extensions Certificate"错误

c++ - 通过 Rcpp 和 bit64 R 包将最大的 int64_t 变量值从 C++ 传递到 R

C++,通过 const 引用访问 std::map 元素

c++ - 将字符串转换为长

c++ - 如何在 cout 语句后打印 void 函数?

ios - 无论如何在 Objective-C 中有一系列双对象

objective-c - 如何允许用户水平移动按钮和垂直移动表格 View ?

objective-c - @class 用于 typedef 枚举?

python - 请求的模块不可用 : vtkRenderingOpenGL-cmake/OpenCVDetectVTK. cmake:6 (find_package)

ios - SKLightNodes 导致 SpriteKit 中的帧速率问题