How to convert unmanaged CFData to NSData?
As explained in
Working with Cocoa Data Types, you have to convert the unmanaged object to a memory managed object with takeUnretainedValue()
or takeRetainedValue()
.
In your case
if (ABPersonHasImageData(person)) {
let imgData = ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatOriginalSize).takeRetainedValue()
let image = UIImage(data: imgData)
}
because ABPersonCopyImageDataWithFormat()
returns a (+1) retained value.
How do you import a DER certificate in swift?
In Swift, SecCertificateCreateWithData
returns an Unmanaged
type. You need to get the value of the unmanaged reference with takeRetainedValue()
.
let mainbun = NSBundle.pathForResource("mainkey", ofType: "der", inDirectory: "/myapppath")
var key: NSData = NSData(base64EncodedString: mainbun!, options: nil)!
var turntocert: SecCertificateRef =
SecCertificateCreateWithData(kCFAllocatorDefault, key).takeRetainedValue()
The core problem you have is converting CFData
to NSData
. See this question
Error with fetching value from CFArray
You cannot force cast an UnsafePointer<Void>
to a type. You must first convert that void pointer to UnsafePointer<Type>
then take its memory:
let aPerson = UnsafePointer<ABRecordRef>(CFArrayGetValueAtIndex(allPeople, i)).memory
FYI... ABAddressBook
has been deprecated on iOS 9. For new code targeting that OS, use CNContactStore
instead.
Cannot convert value of type 'SecCertificate' to expected argument type 'UnsafeMutablePointer UnsafeRawPointer? !'
The line causing the error, is for creating a CFArray
.
One simple way to create a CFArray
, is to use bridging the Swift Array
to CFArray
.
Try changing the line:
let certArrayRef = CFArrayCreate(nil, cert, 1, nil)
To:
let certArrayRef = [cert] as CFArray
Related Topics
Swift Generics Error: Cannot Convert Value of Type 'Type<T>' to Expected Argument Type 'Type<_>'
Changing Texteditor Background Color in Swiftui for MACos
How How to Perform Multiple Alamofire Requests That Are Finished One After Another
Swift: Dictionaries Inside Array
How to Convert Dispatchtimeinterval to Nstimeinterval (Or Double)
In Swift, How to Wait Until a Server Response Is Received Before I Proceed
How to Specify the Type Information of an Array That Would Hold Swiftui Custom Views
Swift Firebase Storage How to Retrieve Image with Unknow Name(Nsuuid)
Fetch Coredata with One to Many Relationship in Swift
Simple Observable Struct with Rxswift
Cannot Call Value of Non-Function Type 'Ciimage'
Searchbar Problem While Trying to Search Firestore and Reload the Tableview
Differences Generic Protocol Type Parameter VS Direct Protocol Type
Using "Codable" to Set Property Values Doesn't Work Through Inheritance