简体   繁体   English

Cocoa异步套接字教程代码从Swift 2转换为Swift 3

[英]Cocoa Async Socket Tutorial Code Conversion from Swift 2 to Swift 3

I am working through a Swift 2 Cocoa Async Socket tutorial which was downloaded from https://github.com/nickyhuyskens/SwiftCocoaAsyncSocketTutorial It was written in Swift 2 and I am working with Xcode 8 / Swift 3. The conversion seem fairly good with only a few correction except for one place where I cannot seem to make the needed correction. 我正在通过从https://github.com/nickyhuyskens/SwiftCocoaAsyncSocketTutorial下载的Swift 2 Cocoa Async Socket教程。它是用Swift 2编写的,我正在使用Xcode 8 / Swift 3.转换看起来相当不错除了一个我似乎无法做出必要修正的地方之外,还有一些修正。

Original Swift 2 code: 原始Swift 2代码:

func socket(_ sock: GCDAsyncSocket!, didRead data: Data!, withTag tag: Int)
 {
    if tag == 1 {
         var bodyLength: Int16 = 0
         data.getBytes(&bodyLength, length: sizeof(Int16))
         print("Header received with bodylength: \(bodyLength)")
         socket.readDataToLength(UInt(bodyLength), withTimeout: -1, tag: 2)
    } else if tag == 2 {             
         let packet = NSKeyedUnarchiver.unarchiveObjectWithData(data) as! Packet
         PacketHandler.HandlePacket(packet)
         socket.readDataToLength(UInt(sizeof(Int16)), withTimeout: -1, tag: 1)
        socket.readData(toLength: UInt(MemoryLayout<Int16>.size), withTimeout: -1, tag: 1)
    }
}

My Swift 3 changes: 我的Swift 3改变了:

func socket(_ sock: GCDAsyncSocket!, didRead data: Data!, withTag tag: Int) {
   if tag == 1 {
                    var bodyLength: UInt16 = 0
        bodyLength = (UInt16(MemoryLayout<Data>.size))
        print("Header received with bodylength: \(bodyLength)")
        socket.readData(toLength: UInt(bodyLength), withTimeout: -1, tag: 2)
    } else if tag == 2 {
        let packet = NSKeyedUnarchiver.unarchiveObject(with: data) as! Packet
        PacketHandler.HandlePacket(packet)
        socket.readData(toLength: UInt(MemoryLayout<Int16>.size), withTimeout: -1, tag: 1)
    }
}

When it gets to the line 当它到达线

let packet = NSKeyedUnarchiver.unarchiveObject(with: data) as! Packet

the result is a "fatal error: unexpectedly found nil while unwrapping an Optional value". 结果是“致命错误:在展开Optional值时意外发现nil”。 I have been looking at posts involving the changes in Swift 3 around change to getting the "sizeof" data and am not sure if the change to "MemoryLayout" was correct. 我一直在查看涉及Swift 3变化的帖子,以获取“sizeof”数据,并且不确定“MemoryLayout”的更改是否正确。 Also I have tried a number of changes following posts involving Swift 3 changes to the use of the NSKeyedUnarchiver.unarchiveObjectWithData functionality but those also do not provide a working solution. 此外,我在使用Swift 3更改使用NSKeyedUnarchiver.unarchiveObjectWithData功能的帖子之后尝试了一些更改,但这些也没有提供可行的解决方案。

Any assistance or pointing to where the conversion solution can be found would be greatly appreciated. 任何帮助或指向可以找到转换解决方案的地方将不胜感激。

print(MemoryLayout<Data>.size == 8)

prints 版画

true

Are You sure that this is what do You expect? 你确定这是你期望的吗? It is the size of Data type, not the number of stored bytes. 它是数据类型的大小,而不是存储的字节数。

the size of data (for example) 数据的大小(例如)

let data = Data(bytes: [1,2,3,4,5,6,7,8,9,0])
print(data.count)

prints 版画

10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM