Domanda

I have a C++ library code base that uses Carbon. I need to upgrade it to Cocoa because I'm getting these warnings:

warning: 'FSPathMakeRef' is deprecated: first deprecated in OS X 10.8 [-Wdeprecated-declarations] warning: 'FSGetCatalogInfo' is deprecated: first deprecated in OS X 10.8 [-Wdeprecated-declarations] warning: 'FSSetCatalogInfo' is deprecated: first deprecated in OS X 10.8 [-Wdeprecated-declarations]

Here is the place where I'm using these functions:

#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

My questions are:

1) How do I change:

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

not to use the FSPathMakeRef function?

2) How do I change:

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

not to use the FSGetCatalogInfo function?

3) How do I change:

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

not to use the FSSetCatalogInfo function?

È stato utile?

Soluzione

NSFileManager has methods to get and set file attributes like the file type and creator.

Example (error checking omitted for brevity):

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];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top