2 votes

Impossible de convertir une valeur de type 'NSRange' (alias 'NSRange') en un argument attendu de type 'Range<Index>' (alias 'Range<Int>').

J'essaie de mettre en place Easy Game Center lien mais j'obtiens cette erreur :

Impossible de convertir une valeur de type 'NSRange' (aka 'NSRange') en une valeur attendue. type d'argument attendu 'Range<Index>' (aka 'Range<Int>')

Voici le code :

 static func unarchive(_ data: Data!) -> Packet {
    var archivedPacket = ArchivedPacket(index: 0, numberOfPackets: 0, nameLength: 0) //, dataLength: 0
    let archivedStructLength = sizeof(ArchivedPacket)

    let archivedData = data.subdata(in: NSMakeRange(0, archivedStructLength))
    (archivedData as NSData).getBytes(&archivedPacket)

    let nameRange = NSMakeRange(archivedStructLength, Int(archivedPacket.nameLength))
    let nameData = data.subdata(in: nameRange)
    let name = NSString(data: nameData, encoding: String.Encoding.utf8) as! String
    let packet = Packet(name: name, index: archivedPacket.index, numberOfPackets: archivedPacket.numberOfPackets)

    return packet
}

edit : voici le code complet de la structure :

import Foundation
/**
*  Packet
*/
struct Packet {
    var name: String
    var index: Int64
    var numberOfPackets: Int64

    /**
    *  Struc
    */
    struct ArchivedPacket {
        var index : Int64
        var numberOfPackets : Int64
        var nameLength : Int64
    }
    /**
    Archive Packet

    - returns: NSData
    */
    func archive() -> Data {

        var archivedPacket = ArchivedPacket(index: Int64(self.index), numberOfPackets: Int64(self.numberOfPackets), nameLength: Int64(self.name.lengthOfBytes(using: String.Encoding.utf8)))

        let metadata = Data(
            bytes: UnsafePointer<UInt8>(&archivedPacket),
            count: sizeof(ArchivedPacket)
        )

        let archivedData = Data(data: metadata) as Data
        archivedData.append(name.data(using: String.Encoding.utf8, allowLossyConversion: false)!)

        return archivedData
    }
    /**
    Unarchive Packet

    - parameter data: NSData

    - returns: Packet
    */
    static func unarchive(_ data: Data!) -> Packet {
        var archivedPacket = ArchivedPacket(index: 0, numberOfPackets: 0, nameLength: 0) //, dataLength: 0
        let archivedStructLength = sizeof(ArchivedPacket)

        let archivedData = data.subdata(in: NSMakeRange(0, archivedStructLength))
        (archivedData as NSData).getBytes(&archivedPacket)

        let nameRange = NSMakeRange(archivedStructLength, Int(archivedPacket.nameLength))
        let nameData = data.subdata(in: nameRange)
        let name = NSString(data: nameData, encoding: String.Encoding.utf8) as! String
        let packet = Packet(name: name, index: archivedPacket.index, numberOfPackets: archivedPacket.numberOfPackets)

        return packet
    }
}

4voto

Zoff Dino Points 5010

Vous compliquez excessivement les choses en convertissant en NSRange . Cela fonctionne :

let archivedData = data.subdata(in: 0..<archivedStructLength)
let nameData = data.subdata(in: archiveStructLength..<(archiveStructLength + Int(archivedPacket.nameLength)))

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X