2016-07-26 9 views
1

私はしばらくの間問題に苦しんできました。withUnsafeMutablePointerはコンパイルされません

private func generateIdentity (base64p12 : NSData, password : String?) { 
    let p12KeyFileContent = NSData(data: base64p12) 

    let options = [String(kSecImportExportPassphrase):password ?? ""] 
    var citems: CFArray? = nil 
    let resultPKCS12Import = withUnsafeMutablePointer(&citems) { citemsPtr in // line with the error 
     SecPKCS12Import(p12KeyFileContent!, options, citemsPtr) 
    } 
    if (resultPKCS12Import != errSecSuccess) { 
     print(resultPKCS12Import) 
     return 
    } 

    let items = citems! as NSArray 
    let myIdentityAndTrust = items.objectAtIndex(0) as! NSDictionary 
    let identityKey = String(kSecImportItemIdentity) 

    identity = myIdentityAndTrust[identityKey] as! SecIdentityRef 
    hasCertificate = true 
    print("cert cre", identity) 
} 

XCodeのは、と言われます:

がCFArray inoutの」型の値を変換できません。この他の一つではないのに対し、

private func generateIdentity (base64p12 : String, password : String?, url : NSURL) { 

    let p12KeyFileContent = NSData(base64EncodedString: base64p12, options: NSDataBase64DecodingOptions(rawValue: 0)) 
    if (p12KeyFileContent == nil) { 
     NSLog("Cannot read PKCS12 data") 
     return 
    } 

    let options = [String(kSecImportExportPassphrase):password ?? ""] 
    var citems: CFArray? = nil 
    let resultPKCS12Import = withUnsafeMutablePointer(&citems) { citemsPtr in 
     SecPKCS12Import(p12KeyFileContent!, options, citemsPtr) 
    } 
    if (resultPKCS12Import != errSecSuccess) { 
     print(resultPKCS12Import) 
     return 
    } 

    let items = citems! as NSArray 
    let myIdentityAndTrust = items.objectAtIndex(0) as! NSDictionary 
    let identityKey = String(kSecImportItemIdentity) 

    identity = myIdentityAndTrust[identityKey] as! SecIdentityRef 
    hasCertificate = true 
    print("cert cre", identity) 
} 

コンパイル:私は、なぜこのコードを思ったんだけど? ' (別名 'inoutオプション')を予想引数型 'inout _'に設定します。

基本的にはNSData引数を使用していたため、2つのコードがcitems変数でどのように異なるかは実際には分かりませんこの関数はbase64文字列変換をNSDataにバイパスします。

答えて

1

エラーメッセージがスーパー紛らわしいですが、エラーの原因はここに常駐:

SecPKCS12Import(p12KeyFileContent!, options, citemsPtr) 

をあなたの第二の例では、let p12KeyFileContent = NSData(data: base64p12)を宣言すると、p12KeyFileContentの種類はNSData、ないNSData?です。したがって、p12KeyFileContentには!を使用できません。

SecPKCS12Import(p12KeyFileContent, options, citemsPtr) 

!を削除。)


つ以上:

は、ラインを変更してみてください。

通常に電話するには、withUnsafeMutablePointerを使用する必要はありません。これに

let resultPKCS12Import = withUnsafeMutablePointer(&citems) { citemsPtr in // line with the error 
    SecPKCS12Import(p12KeyFileContent!, options, citemsPtr) 
} 

は、これらの3行(2番目の例では)を交換してください

let resultPKCS12Import = SecPKCS12Import(p12KeyFileContent, options, &citems) 

最初のコード例は、同様に書き換えることができます。

関連する問題