发送到其他设备后 NSData 丢失

NSData missing after sent to other device

在设备之间传输时数据丢失。怀疑它真的丢失了 - 我确定我做错了什么 - 但是什么?!

4阶梯方法。前 3 个步骤非常有效,我能够验证数据是否可以解析(在发送之前)。 我收到了数据,但不是全部?解析时我可以确认数据类型,但不能确认类别信息(性别、类别、子类别)——为什么?步骤:

  1. 生成和存储数据(本地)
  2. 验证数据是否正确存储
  3. 将数据发送到另一台设备(iPhone)
  4. 接收数据并解析信息 --> 不成功

1.生成数据

func selectCategories(){
    //Fixed data for testing
    let gender = "Women"
    let category = "Shoes"
    let subCategory = "Pumps"

    print("SENDING - Gender: \(gender), Category: \(category), SubCategory: \(subCategory)")

    sendCategories(gender, category: category, subCategory: subCategory)

}

2。验证存储的数据

func sendCategories(gender: String, category: String, subCategory: String){
    var messageCategory = MessageCategory(message: Message(messageType: MessageType.Category), gender: gender, category: category, subCategory: subCategory)
    let data = NSData(bytes: &messageCategory, length: sizeof(MessageCategory))
    print("SEND - LENGTH: \(data.length)")

    // VERIFY DATA STORED SUCCESFULLY (FOR TESTING PURPOSES ONLY)
    let messageCategoryFromData = UnsafePointer<MessageCategory>(data.bytes).memory
    if messageCategoryFromData.message.messageType == MessageType.Category{

    print("SENDING - Gender: \(messageCategoryFromData.gender), Category: \(messageCategoryFromData.category), SubCategory: \(messageCategoryFromData.subCategory)")

    let temp_gender = messageCategoryFromData.gender
    let temp_category = messageCategoryFromData.category
    let temp_subCategory = messageCategoryFromData.subCategory
    let message = messageCategoryFromData.message

    // SUCCESSFULLY PRINTS THE GENDER ETC WITH CORRECT INFORMATION -> DATA PARSING WORKS PRIOR TO SENDING DATA
    print("RECEIVED (PRIOR TO SENDING). Gender: \(temp_gender) Category: \(temp_category) SubCategory: \(temp_subCategory)")
    }
    else{
        print("WRONG MESSAGE TYPE")
    }

    // SEND THE DATA
    sendData(data)
}

3。发送数据

func sendData(data: NSData) {
    print("SENDING DATA")
    var sendDataError: NSError?
    let gameKitHelper = GameKitHelper.sharedInstance

    if let multiplayerMatch = gameKitHelper.multiplayerMatch {
        do {
            try multiplayerMatch.sendDataToAllPlayers(data, withDataMode: GKMatchSendDataMode.Reliable)
        } catch let error as NSError {
            print("Error while sending data: \(error)")
        } catch {
            print("Catch all errors: \(error)")
        }
    }
}

4.接收数据和解析信息(错误)

func matchReceivedData(match: GKMatch, data: NSData, fromPlayer player: String) {

    var message = UnsafePointer<Message>(data.bytes).memory

    if message.messageType == MessageType.Category {

        let messageCategory = UnsafePointer<MessageCategory>(data.bytes).memory

        print("MESSAGE: \(messageCategory.message)")   <-- SUCCESSFUL
        print("DATA TYPE: \(messageCategory.message.messageType)")    <-- SUCCESSFUL
        print("RECEIVED - LENGTH: \(data.length)")        <-- SUCCESSFUL

        let gender = messageCategory.gender            <-- BECOMES NIL
        let category = messageCategory.category        <-- BECOMES NIL
        let subCategory = messageCategory.subCategory  <-- BECOMES NIL

        delegate?.setCategories(g, category: c, subCategory: s)

        print("RECEIVING - Gender: \(gender), Category: \(category), subCategory\(subCategory)")
    }
} 

支撑结构

enum MessageType: Int {
    case RandomNumber, Category, GameBegin, Move, LapComplete, GameOver
}

struct Message {
    let messageType: MessageType
}

struct MessageCategory {
    let message: Message
    let gender: String
    let category: String
    let subCategory: String
}

我稍微修改了你的代码

import XCPlayground
import Foundation

enum MessageType: Int {
    case RandomNumber, Category, GameBegin, Move, LapComplete, GameOver
}

struct Message {
    let messageType: MessageType
}

struct MessageCategory {
    let message: Message
    let gender: String
    let category: String
    let subCategory: String
}

func sendCategories(gender: String, category: String, subCategory: String)->NSData {
    var messageCategory = MessageCategory(message: Message(messageType: MessageType.Category), gender: gender, category: category, subCategory: subCategory)
    let data = NSData(bytes: &messageCategory, length: sizeof(MessageCategory))
    print("SEND - LENGTH: \(data.length)")

    // VERIFY DATA STORED SUCCESFULLY (FOR TESTING PURPOSES ONLY)
    let messageCategoryFromData = UnsafePointer<MessageCategory>(data.bytes).memory
    if messageCategoryFromData.message.messageType == MessageType.Category{

        print("SENDING - Gender: \(messageCategoryFromData.gender), Category: \(messageCategoryFromData.category), SubCategory: \(messageCategoryFromData.subCategory)")

        let temp_gender = messageCategoryFromData.gender
        let temp_category = messageCategoryFromData.category
        let temp_subCategory = messageCategoryFromData.subCategory
        let message = messageCategoryFromData.message

        // SUCCESSFULLY PRINTS THE GENDER ETC WITH CORRECT INFORMATION -> DATA PARSING WORKS PRIOR TO SENDING DATA
        print("RECEIVED (PRIOR TO SENDING). Gender: \(temp_gender) Category: \(temp_category) SubCategory: \(temp_subCategory)")
    }
    else{
        print("WRONG MESSAGE TYPE")
    }

    // SEND THE DATA
    //sendData(data)
    return data
}

let data = sendCategories("gender", category: "category", subCategory: "subcategory")

func matchReceivedData(data: NSData, fromPlayer player: String) {

    var message = UnsafePointer<Message>(data.bytes).memory

    if message.messageType == MessageType.Category {

        let messageCategory = UnsafePointer<MessageCategory>(data.bytes).memory

        print("MESSAGE: \(messageCategory.message)")
        print("DATA TYPE: \(messageCategory.message.messageType)")
        print("RECEIVED - LENGTH: \(data.length)")

        let gender = messageCategory.gender
        let category = messageCategory.category
        let subCategory = messageCategory.subCategory


        print("RECEIVING - Gender: \(gender), Category: \(category), subCategory: \(subCategory)")
    }
}

matchReceivedData(data, fromPlayer: "player")

/*
SEND - LENGTH: 80
SENDING - Gender: gender, Category: category, SubCategory: subcategory
RECEIVED (PRIOR TO SENDING). Gender: gender Category: category SubCategory: subcategory
MESSAGE: Message(messageType: MessageType.Category)
DATA TYPE: Category
RECEIVED - LENGTH: 80
RECEIVING - Gender: gender, Category: category, subCategory: subcategory
*/

我的代码中没有'sending'。如您所见,问题出在您 'sending->receiving' 部分。

什么是匹配参数?它没有在你的函数中使用..

func matchReceivedData(match: GKMatch, data: NSData, fromPlayer player: String)

错误是我使用 Struct 来处理数据。但是在阅读之后我意识到结构只指向数据。这导致我的 String 数据未与 NSData 一起发送(FloatInt 工作正常)。

解决方案是不使用 struct 来处理对象数据,而是将其作为原始 NSData 传递,然后始终包含一部分数据,告诉它是哪种类型的数据,然后使用正确的解析。