本文介绍了CFURLCreateDataAndPropertiesFromResource已弃用.并寻找替代品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了Apple的Load Preset Demo示例代码中包含的其他功能外,现在不建议调用CFURLCreateDataAndPropertiesFromResource.但是我找不到它的替代品-单击选项或查看引用都不能告诉我,这不再是一件完成的事情.

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;

推荐答案

对于属性: CFURLCopyResourcePropertiesForKeys示例属性:kCFURLFileSizeKeykCFURLContentModificationDateKey,或具有[NSURL resourceValuesForKeys:error:]的Foundation样式

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

有关数据: +[NSData dataWithContentsOfURL:options:error:].

它们没有被记录为替代品,即AFAIK.这些更新的API大多已经存在了几年.

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

修改

在此示例中,您在编辑中发布了该程序,该程序不请求属性,因此只需要URL presetURL上的数据.

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
}

这篇关于CFURLCreateDataAndPropertiesFromResource已弃用.并寻找替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 21:19