John Difool
John Difool

Reputation: 5702

CFSocketCreate or constructor method missing. How do you create a CFSocket using Swift?

Any method call to instantiate a CFSocket seem to be missing from the import CoreFoundation

import CoreFoundation

var socket:CFSocket = CFSocket()

The reference documentation doesn't show anything when swift only is selected:

Creating Sockets

4 Objective-C symbols hidden

How can I instantiate a CFSocket under Swift?

Upvotes: 4

Views: 2223

Answers (2)

zhujian
zhujian

Reputation: 1

replace callout with closure like below:

let socket = CFSocketCreate(kCFAllocatorDefault,
                          PF_INET, 
                          SOCK_STREAM, 
                          IPPROTO_TCP, 
                          2,
                          { (socket,_,_,_,_) in },
                          nil)

Upvotes: 0

Denis Kohl
Denis Kohl

Reputation: 750

i had the same Problem. It is a CF struct not an Object so you have no a constructor. You can use CFSocketCreate.

Some think like this:

  let socket : CFSocket = CFSocketCreate(_ allocator: CFAllocator!,
              _ protocolFamily: Int32,
              _ socketType: Int32,
              _ `protocol`: Int32,
              _ callBackTypes: CFOptionFlags,
              _ callout: CFSocketCallBack,
              _ context: UnsafePointer<CFSocketContext>)

Reference

Upvotes: 1

Related Questions