gpt4 book ai didi

ios - 将 NSData 转换为! swift 2.0 中的 CFDataRef

转载 作者:可可西里 更新时间:2023-11-01 05:01:38 25 4
gpt4 key购买 nike

我在代码中有一行已被弃用,XCode 中有建议用什么替换它,但我无法理解其中的区别,这些是我的三行有效:

let path = NSBundle.mainBundle().pathForResource("example", ofType: ".p12")
let pkcs12Data = NSData.dataWithContentsOfMappedFile(path!)
let cf: CFDataRef = pkcs12Data as! CFDataRef

现在根据警告和建议,我将代码更改为:

let path = NSBundle.mainBundle().pathForResource("example", ofType: ".p12")
let pkcs12Data = NSData(contentsOfFile: path!)
let cf: CFDataRef = pkcs12Data as! CFDataRef

这给我一个错误:

EXC_BAD_INSTRUCTION (CODE=EXC_I386_INVOP SUBCODE=0x0)

最佳答案

稍微安全一点的版本:

guard
let url = NSBundle.mainBundle().URLForResource("example", withExtension: ".p12"),
let data = NSData(contentsOfURL: url)
else { // Do something because you couldn't get the file or convert it to NSData }

let dataPtr = CFDataCreate(kCFAllocatorDefault, UnsafePointer<UInt8>(data.bytes), data.length)

请注意,使用基于文件的 URL 而不是字符串路径。

When deciding which routines to call, choose ones that let you specify paths using NSURL objects over those that specify paths using strings. Most of the URL-based routines were introduced in OS X v10.6 and later and were designed from the beginning to take advantage of technologies like Grand Central Dispatch. This gives your code an immediate advantage on multicore computers while not requiring you to do much work.

来自 File System Programming Guide

关于ios - 将 NSData 转换为! swift 2.0 中的 CFDataRef,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32883266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com