我在Swift中编写Socket编程。对于所有CoreFoundation常量,我收到错误:
使用未解析的标识符
我已导入CoreFoundation
import Foundation
import CoreFoundation
import CFNetwork
添加了corefoundation框架
但以上都没有解决错误。
添加代码
import Cocoa
import Foundation
import CoreFoundation
import CFNetwork
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
CFSocketCreate(nil, // Default allocator
PF_INET, // Protocol Family
SOCK_STREAM, // Socket Type
IPPROTO_TCP, // Protocol
kCFSocketConnectCallBack, // Callback flags
(MyCFSocketCallback as! CFSocketCallBack), // Callback method
nil)
}
func MyCFSocketCallback(sref: CFSocketRef!, type: CFSocketCallBackType, inAddress: CFDataRef!, pData: UnsafePointer<Void>, pInfo: UnsafeMutablePointer<Void>) {
switch type {
case kCFSocketConnectCallBack:
NSLog("Strem Event Open Completed")
break;
case kCFSocketAcceptCallBack:
NSLog("Accept ")
break;
default:
NSLog("received unexpected CFReadStream callback, CFStreamEventType")
}
}
Code Snippet的截屏: