好吧,这是我在使用CGImageSource时遇到的情况,并注意到CFDictionary和NSDictionary之间的免费桥接在某些情况下似乎遇到了问题.我已设法构建以下示例以显示我的意思:
func optionalProblemDictionary() -> CFDictionary? {
let key = "key"
let value = "value"
var keyCallBacks = CFDictionaryKeyCallBacks()
var valueCallBacks = CFDictionaryValueCallBacks()
let cfDictionary = CFDictionaryCreate(kCFAllocatorDefault, UnsafeMutablePointer(unsafeAddressOf(key)), UnsafeMutablePointer(unsafeAddressOf(value)), 1, &keyCallBacks, &valueCallBacks)
return cfDictionary
}
Run Code Online (Sandbox Code Playgroud)
相当简单(有点傻)但它的功能返回和可选的CFDictionary.尝试从此函数创建NSDictionary时,"fun"开始:
以下为什么不工作?
if let problemDictionary = optionalProblemDictionary() as? NSDictionary {
print(problemDictionary) // never enters, no warnings, compiles just fine
}
Run Code Online (Sandbox Code Playgroud)
虽然这很好吗?
if let cfDictionary = optionalProblemDictionary() {
let problemDictionary = cfDictionary as NSDictionary
print(problemDictionary)
}
Run Code Online (Sandbox Code Playgroud)
XCode 7.0(7A220)
根据文档 CFDictionaryCreate用于实例化CFDictionary swift.
func CFDictionaryCreate(_ allocator: CFAllocator!,
_ keys: UnsafeMutablePointer<UnsafePointer<Void>>,
_ values: UnsafeMutablePointer<UnsafePointer<Void>>,
_ numValues: CFIndex,
_ keyCallBacks: UnsafePointer<CFDictionaryKeyCallBacks>,
_ valueCallBacks: UnsafePointer<CFDictionaryValueCallBacks>) -> CFDictionary!
Run Code Online (Sandbox Code Playgroud)
我怎样才能创建keys和values参数?到目前为止,我已尝试使用swift的String类型,希望它会自动转换为适当的类型:
import Foundation
var keys : [String] = ["key1", "key2"]
var values : [String] = ["value1", "value2"]
var keyCallbacks = kCFTypeDictionaryKeyCallBacks
var valueCallbacks = kCFTypeDictionaryValueCallBacks
var dict : CFDictionary = CFDictionaryCreate(kCFAllocatorDefault,
&keys, &values, 2, &keyCallbacks, &valueCallbacks)
Run Code Online (Sandbox Code Playgroud)
不幸的是我收到一个错误,说String不是keys和values数组元素的正确类型:
main.swift:41:2: error: 'String' is …Run Code Online (Sandbox Code Playgroud) 将Swift 3中的Dictionary转换为CFDictionary的正确方法是什么?这样写是合法的吗?
var dictionary:Dictionary<NSString, Int> = [:]
Run Code Online (Sandbox Code Playgroud)
然后
dictionary as CFDictionary
Run Code Online (Sandbox Code Playgroud) 我正在将一些旧的Objective-C代码转换为Swift,所以我可以放弃一些不赞成使用的方法,但是我一直在崩溃,到目前为止我似乎无法弄清楚是什么导致了它.我从P12证书中获取私钥,这个方法似乎工作正常,直到我到达实际需要从CFArray获取字典的部分,即使数组中有值,应用程序也会崩溃.这是我的代码:
func privateKeyFromCertificate(p12Name: String, withPassword password: String) -> SecKeyRef {
let resourcePath: String = NSBundle.mainBundle().pathForResource(p12Name, ofType: "p12")!
let p12Data: NSData = NSData(contentsOfFile: resourcePath)!
let key : NSString = kSecImportExportPassphrase as NSString
let options : NSDictionary = [key : password]
var privateKeyRef: SecKeyRef? = nil
var items : CFArray?
let securityError: OSStatus = SecPKCS12Import(p12Data, options, &items)
let description : CFString = CFCopyDescription(items)
print(description)
if securityError == noErr && CFArrayGetCount(items) > 0 {
let objects : CFDictionaryRef = CFArrayGetValueAtIndex(items, 0) as! CFDictionaryRef …Run Code Online (Sandbox Code Playgroud) 我想用 Swift 创建一些 PDF 页面。
这是我创建 PDF 页面的代码
// Initialize context and other stuff
CGPDFContextBeginPage(pdfContext, aDictionary);
CGContextDrawPDFPage(pdfContext, pdfPageRef)
CGPDFContextEndPage(pdfContext)
Run Code Online (Sandbox Code Playgroud)
我对“aDictionary”变量有疑问。我想在 kCGPDFContextMediaBox 键下添加媒体框(CGRect 类型变量)值。该文档告诉我以下内容:
The media box for the document or for a given page. This key is optional. If present, the value of this key must be a CFData object that contains a CGRect (stored by value, not by reference).
在 Objective-C 中,从 CGRect 创建 CFData 相当容易,但在 Swift 中我不知道如何创建它。这是我的 Objective-C 代码:
CGRect mediaBox = CGRectZero;
CFDataCreate(kCFAllocatorDefault, (const UInt8 *)&mediaBox, sizeof(mediaBox)); …Run Code Online (Sandbox Code Playgroud) cfdictionary ×5
swift ×5
cfdata ×1
cfstring ×1
ios ×1
macos ×1
nsdictionary ×1
swift2 ×1
swift3 ×1