CFURLCreateDataAndPropertiesFromResource deprecated. And looking for substitute

前提是你 提交于 2019-12-01 06:54:52

问题


Along with a bunch of other things included in Apple's Load Preset Demo sample code, the call to CFURLCreateDataAndPropertiesFromResource is now deprecated. But I can't find a substitute for it - neither an option-click nor a look at the reference tell me any more than that it is no longer the done thing.

CFDataRef propertyResourceData = 0;
Boolean status;
SInt32 errorCode = 0;
OSStatus result = noErr;

// Read from the URL and convert into a CFData chunk
status = CFURLCreateDataAndPropertiesFromResource (
                                                   kCFAllocatorDefault,
                                                   (__bridge CFURLRef) presetURL,
                                                   &propertyResourceData,
                                                   NULL,
                                                   NULL,
                                                   &errorCode
                                                   );


NSAssert (status == YES && propertyResourceData != 0, @"Unable to create data and properties from a preset. Error code: %d '%.4s'", (int) errorCode, (const char *)&errorCode);

// Convert the data object into a property list
CFPropertyListRef presetPropertyList = 0;
CFPropertyListFormat dataFormat = 0;
CFErrorRef errorRef = 0;
presetPropertyList = CFPropertyListCreateWithData (
                                                   kCFAllocatorDefault,
                                                   propertyResourceData,
                                                   kCFPropertyListImmutable,
                                                   &dataFormat,
                                                   &errorRef
                                                   );

// Set the class info property for the Sampler unit using the property list as the value.
if (presetPropertyList != 0) {

    result = AudioUnitSetProperty(
                                  self.samplerUnit,
                                  kAudioUnitProperty_ClassInfo,
                                  kAudioUnitScope_Global,
                                  0,
                                  &presetPropertyList,
                                  sizeof(CFPropertyListRef)
                                  );

    CFRelease(presetPropertyList);
}

if (errorRef) CFRelease(errorRef);
CFRelease (propertyResourceData);

return result;

回答1:


For the properties: CFURLCopyResourcePropertiesForKeys example property: kCFURLFileSizeKey and kCFURLContentModificationDateKey, or Foundation-style with [NSURL resourceValuesForKeys:error:].

For the data: +[NSData dataWithContentsOfURL:options:error:].

They're not documented as replacements, AFAIK. Most of these newer replacement APIs have been around for a few years now.

Edit

In this example you posted in the edit, the program makes no request for properties, so you just want the data at the URL presetURL.

You can achieve this by:

NSURL * presetURL = ...;
// do review these options for your needs. you can make great
// optimizations if you use memory mapping or avoid unnecessary caching.
const NSDataReadingOptions DataReadingOptions = 0;
NSError * outError = nil;
NSData * data = [NSData dataWithContentsOfURL:presetURL
                                      options:DataReadingOptions
                                        error:&outError];

const bool status = nil != data; // << your `status` variable

if (!status) {
 // oops - an error was encountered getting the data see `outError`
}
else {
 // use the data
}



回答2:


I found that I could remove even more code by using just the following:

OSStatus result = noErr;
NSData* data = [NSData dataWithContentsOfURL:presetURL];
id propertyList = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:NULL error:NULL];

// Set the class info property for the Sampler unit using the property list as the value.
if (propertyList) {
    result = AudioUnitSetProperty(
                                  self.samplerUnit,
                                  kAudioUnitProperty_ClassInfo,
                                  kAudioUnitScope_Global,
                                  0,
                                  (__bridge CFPropertyListRef)propertyList,
                                  sizeof(CFPropertyListRef)
                                  );
}

return result;



回答3:


I ended up using this code https://developer.apple.com/library/mac/technotes/tn2283/_index.html#//apple_ref/doc/uid/DTS40011217-CH1-TNTAG2

- (OSStatus) loadSynthFromPresetURL: (NSURL *) presetURL {

OSStatus result = noErr;

AUSamplerInstrumentData auPreset = {0};

auPreset.fileURL = (__bridge CFURLRef) presetURL;
auPreset.instrumentType = kInstrumentType_AUPreset;

result = AudioUnitSetProperty(self.samplerUnit,
                              kAUSamplerProperty_LoadInstrument,
                              kAudioUnitScope_Global,
                              0,
                              &auPreset,
                              sizeof(auPreset));

return result;


来源:https://stackoverflow.com/questions/19020074/cfurlcreatedataandpropertiesfromresource-deprecated-and-looking-for-substitute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!