处理包含Swift中包含数组的结构的指针

时间:2015-06-17 20:42:00

标签: arrays swift pointers struct crash

我正在尝试在Swift中编写一个Mac应用程序,它通过http://rsms.me/peertalk/提供的名为PeerTalk的库与iOS设备进行通信,因此我一直在尝试在Swift中重写PeerTalk示例项目。我的Swift重写正确连接和接收来自iOS设备的消息,但无法在不崩溃的情况下读取消息。

struct PTExampleTextFrame {
    let length: UInt32
    let utf8text: [UInt8]
}

func ioFrameChannel(channel: PTChannel?, didReceiveFrameOfType type: UInt32, tag: UInt32, payload: PTData?) {
    if (type == PTExampleFrameTypeDeviceInfo) {
        let deviceInfo: NSDictionary? = NSDictionary(contentsOfDispatchData: payload!.dispatchData)
        self.presentMessage(NSString(format: "Connected to %@", deviceInfo!.description), isStatus:true)
    } else if (type == PTExampleFrameTypeTextMessage) {

        let textFramePointer = UnsafeMutablePointer<PTExampleTextFrame>(payload!.data)
        let textFrameOptional: PTExampleTextFrame? = textFramePointer.memory
        let textFrame: PTExampleTextFrame

        if textFrameOptional != nil {
            textFrame = textFrameOptional!
            println(ntohl(textFrame.length))
            println(textFrame.utf8text)
        } else {
            println("Recieved bad packet")
        }

    } else if (type == PTExampleFrameTypePong) {
        self.pongWithTag(tag, error:nil)
    }

}

下面的结构和块是我遇到问题的唯一部分。

    let textFramePointer = UnsafeMutablePointer<PTExampleTextFrame>(payload!.data)
    let textFrameOptional: PTExampleTextFrame? = textFramePointer.memory
    let textFrame: PTExampleTextFrame

    if textFrameOptional != nil {
        textFrame = textFrameOptional!
        println(ntohl(textFrame.length))
        println(textFrame.utf8text)
    } else {
        println("Recieved bad packet")
    }

当我的应用收到消息时,会成功调用ioFrameChannel函数,并在崩溃之前打印正确长度的消息

Thread 1: EXC_BAD_ACCESS (code=1, address=0x10)

当行打印textFrame.utf8text运行时。

使用不包含数组的结构替换struct会按预期方式打印消息第一个字符的UTF-8表示形式。

struct PTExampleTextFrame {
    let length: UInt32
    let utf8text: UInt8
}

上述工作在Objective C中,如下面的示例项目中所述

//PTExampleProtocol.h
typedef struct _PTExampleTextFrame {
  uint32_t length;
  uint8_t utf8text[0];
} PTExampleTextFrame;

//PTAppDelegate.m
- (void)ioFrameChannel:(PTChannel*)channel didReceiveFrameOfType:(uint32_t)type tag:(uint32_t)tag payload:(PTData*)payload {
  //NSLog(@"received %@, %u, %u, %@", channel, type, tag, payload);
  if (type == PTExampleFrameTypeDeviceInfo) {
    NSDictionary *deviceInfo = [NSDictionary dictionaryWithContentsOfDispatchData:payload.dispatchData];
    [self presentMessage:[NSString stringWithFormat:@"Connected to %@", deviceInfo.description] isStatus:YES];
  } else if (type == PTExampleFrameTypeTextMessage) {
    PTExampleTextFrame *textFrame = (PTExampleTextFrame*)payload.data;
    textFrame->length = ntohl(textFrame->length);
    NSString *message = [[NSString alloc] initWithBytes:textFrame->utf8text length:textFrame->length encoding:NSUTF8StringEncoding];
    [self presentMessage:[NSString stringWithFormat:@"[%@]: %@", channel.userInfo, message] isStatus:NO];
  } else if (type == PTExampleFrameTypePong) {
    [self pongWithTag:tag error:nil];
  }
}

我预计崩溃与Swift有关,不知道阵列什么时候停止。如果没有应用程序崩溃,我如何在Swift中读取整个消息?

1 个答案:

答案 0 :(得分:0)

随着我对Swift的了解越来越多,似乎具有0长度数组的ObjC结构被导入到swift中作为零元组。固定长度数组作为元组导入,因为Swift中不存在固定长度数组。我会假设指向零元组的指针可以用于此目的;但是,我没有测试过,而不是使用这个协议,我只是重写了iOS应用程序并完全改变了协议,甚至不需要这个奇怪的结构。