-1

I'm trying to connect openssl s_server and iOS client using TCP SSL(also, I get another issue with this, maybe this related). Now I'm trying to use this sample code from github for this:

let mainbun = NSBundle.mainBundle().pathForResource("pd-test", ofType: "cer")
var key: NSData = NSData.dataWithContentsOfMappedFile(mainbun!)! as NSData
var cert:SecCertificateRef =
SecCertificateCreateWithData(kCFAllocatorDefault, key).takeRetainedValue()

var err:OSStatus = noErr

let secDict = NSDictionary(
    objects: [kSecClassCertificate,cert],
    forKeys: [kSecClass, kSecValueRef]
)


SecItemAdd(secDict as CFDictionaryRef, nil);

But get errors:

ViewController.swift:67:74: 'AnyObject' is not convertible to 'NSData'; did you mean to use 'as!' to force downcast? 

ViewController.swift:69:9: Value of type 'SecCertificate?' has no member 'takeRetainedValue' 

ViewController.swift:75:23: Value of type 'CFString' does not conform to expected element type 'NSCopying'

Why I can't build valid code(I saw many samples like this, all they throw almost the same errors, e.g. this)? How can I fix it?

Community
  • 1
  • 1
konstantin_doncov
  • 2,725
  • 4
  • 40
  • 100

1 Answers1

0

Why I can't build valid code

It's because the Swift language is constantly evolving; you are using a newer version of Swift than those examples used when they were written. It was valid Swift then, but it is not valid with your version of Swift.

(If you were to download Xcode 8 seed 6, the first line wouldn't even compile; there is now no such thing as NSBundle, no such thing as mainBundle(), and no such thing as pathForResource. This shows how radically the Swift language can change.)

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • How can I downgrade my Swift version which I use in Xcode? Or solve this somehow automatically? Because I have already modified this code manually and now it does not work(or I have [created invalid ssl certificate](http://stackoverflow.com/questions/39185678/cfnetwork-sslhandshake-failed-9807-with-local-ip-in-exception-domains), but some users wrote that it's right way) – konstantin_doncov Aug 30 '16 at 19:50
  • "How can I downgrade my Swift version which I use in Xcode" You would need to know what version of Swift the code is written in, and use the version of Xcode containing that version. All old versions of Xcode are available from Apple. "Or solve this somehow automatically" You might not be able to solve it _totally_ automatically, but the compiler errors you are getting are very clear and easy to fix if you know Swift. As for using OpenSSL, Stack Overflow is full of useful information already. – matt Aug 31 '16 at 00:47