【问题标题】:Swift 3 Unsafe Pointer Ambiguous init and bytesSwift 3 不安全指针不明确的初始化和字节
【发布时间】:2017-03-24 14:58:04
【问题描述】:

在尝试升级涉及将文件导出到 Swift 3 的库时,我遇到了两个主要问题。我已阅读 apple/swift migration page,但仍然不确定要确保我不这样做的最佳方法是什么丢失任何这些字节。很多例子都是转换字符串,但我实际上需要字节。

我遇到的第一个问题是在下面以stream.write 开头的每一行中,UnsafePointer<UIntX>(&var) 返回一个错误说Ambigious use of init

var xbuffer = [UInt8](repeating: 0, count: 2048 + 171*1024)
let stream : OutputStream = OutputStream(toBuffer: &xbuffer, capacity: 2048 + 171*1024);
stream.open();

//Version number (2 bytes)
stream.write(Data(bytes: UnsafePointer<UInt8>(&(self.version)), count: 1).bytes.bindMemory(to: UInt8.self, capacity: Data(bytes: UnsafePointer<UInt8>(&(self.version)), count: 1).count),maxLength: 1);

stream.write(Data(bytes: UnsafePointer<UInt8>(&(self.subVersion)), count: 1).bytes.bindMemory(to: UInt8.self, capacity: Data(bytes: UnsafePointer<UInt8>(&(self.subVersion)), count: 1).count),maxLength: 1);

//Operation ID status code (2 bytes)
var opId_big = CFSwapInt16HostToBig(self.operationId);
stream.write(Data(bytes: UnsafePointer<UInt8>(&(opId_big)), count: 2).bytes.bindMemory(to: UInt8.self, capacity: Data(bytes: UnsafePointer<UInt8>(&(opId_big)), count: 2).count),maxLength: 2);

//Request ID (4 bytes)
requestId = requestId | 0x1;
var rqid_big = CFSwapInt32HostToBig(requestId);
stream.write(Data(bytes: UnsafePointer<UInt8>(&(rqid_big)), count: 4).bytes.bindMemory(to: UInt8.self, capacity: Data(bytes: UnsafePointer<UInt8>(&(rqid_big)), count: 4).count), maxLength: 4);

在创建Data 时,我可以通过对UnsafeRawPointerUnsafeBufferPointer 进行更改来更改此设置。

在我这样做之后,我遇到了另一个问题,Xcode 将打印bytes is unavailable use withUnsafeBytes instead 的错误。

总的来说,我可以使用这样的东西逐个修复次要部分,但是由于所有链式命令,我在进行多项更改和尝试 stackoverflow 解决方案和快速迁移实践时遇到了麻烦,因为需要进行多项更改链式命令。

我感谢任何答案或启示/方向。

Other Migration Page That Was Helpful

StackOverFlow Reference 1

【问题讨论】:

    标签: pointers swift3 void-pointers


    【解决方案1】:

    一般情况下,您无需创建中间Data 即可将字节序列写入OutputStream

        var xbuffer = [UInt8](repeating: 0, count: 2048 + 171*1024)
        xbuffer.withUnsafeMutableBufferPointer{bufferPointer in
            let stream : OutputStream = OutputStream(toBuffer: bufferPointer.baseAddress!, capacity: xbuffer.count);
            stream.open();
    
            //Version number (2 bytes)
            withUnsafeBytes(of: &self.version) {
                stream.write($0.baseAddress!.assumingMemoryBound(to: UInt8.self), maxLength: 1)
            }
    
            withUnsafeBytes(of: &self.subVersion) {
                stream.write($0.baseAddress!.assumingMemoryBound(to: UInt8.self), maxLength: 1)
            }
    
            //Operation ID status code (2 bytes)
            var opId_big = self.operationId.bigEndian
            withUnsafeBytes(of: &opId_big) {
                stream.write($0.baseAddress!.assumingMemoryBound(to: UInt8.self), maxLength: 2)
            }
    
            //Request ID (4 bytes)
            var rqid_big = (requestId | 0x1).bigEndian;
            withUnsafeBytes(of: &rqid_big) {
                stream.write($0.baseAddress!.assumingMemoryBound(to: UInt8.self), maxLength: 4)
            }
        }
    

    【讨论】:

    • 今晚我会试试这个。感谢您的回答。我是 swift 新手,很久以前就用 Objective-C 编写了这个库的核心。如果可行,这将非常有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 2017-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-08
    相关资源
    最近更新 更多