我有一个使用 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/