Search code examples
swiftbytearraysnsdatac-strings

NSData appendBytes in Swift


I'm sort of rewriting application from this link: http://www.raywenderlich.com/12910/how-to-make-a-simple-playing-card-game-with-multiplayer-and-bluetooth-part-3

and I'm stuck with NSData and stuff that has to do with bytes!

What type does 0x64 represents here?

typedef enum
{
    PacketTypeSignInRequest = 0x64,    // server to client
    PacketTypeSignInResponse,          // client to server
    ...
}
PacketType;

Which type should my enum be in swift?

Main problems I encountered are here:

- (void)rw_appendInt32:(int)value
{
    value = htonl(value);
    [self appendBytes:&value length:4];
}

- (void)rw_appendInt16:(short)value
{
    value = htons(value);
    [self appendBytes:&value length:2];
}

- (void)rw_appendInt8:(char)value
{
    [self appendBytes:&value length:1];
}

- (void)rw_appendString:(NSString *)string
{
    const char *cString = [string UTF8String];
    [self appendBytes:cString length:strlen(cString) + 1];
}

I have no idea how should be this written in NSData extension.

Last but not least, how do I pass this 'SNAP' in swift since characters are written almost the same as string in swift?

[data rw_appendInt32:'SNAP'];   // 0x534E4150
[data rw_appendInt32:0];
[data rw_appendInt16:self.packetType];

Also if anyone have any good link on this kind of stuff that has to do with data, int's of different sizes (doesnt have to be swift/objc) please send!


Solution

  • A translation of the NSMutableData extension to Swift could look like this:

    extension NSMutableData {
    
        func appendInt32(value : Int32) {
            var val = value.bigEndian
            self.appendBytes(&val, length: sizeofValue(val))
        }
    
        func appendInt16(value : Int16) {
            var val = value.bigEndian
            self.appendBytes(&val, length: sizeofValue(val))
        }
    
        func appendInt8(value : Int8) {
            var val = value
            self.appendBytes(&val, length: sizeofValue(val))
        }
    
        func appendString(value : String) {
            value.withCString {
                self.appendBytes($0, length: Int(strlen($0)) + 1)
            }
        }
    }
    

    Here value.bigEndian returns the big-endian representation of the given number and corresponds to htonl(), htons() in the Objective-C code. (One could alternatively define a generic method which covers all the various integer cases.)

    From

    [data rw_appendInt16:self.packetType];
    

    we can conclude that the packet types should have an underlying Int16 type, so one would define them as

    enum PacketType : Int16 {
        case SignInRequest = 0x64
        case SignInResponse
        // ...
    }
    

    'SNAP' in

    [data rw_appendInt32:'SNAP'];   // 0x534E4150
    

    is a so-called "four-character constant" or "multi-character literal". It is equal to the 32-bit number built from the four bytes with the ASCII codes of S, N, A, P. Four-character constants seem not to be available in Swift. One could define a custom method which creates an integer from the given characters (see for example https://stackoverflow.com/a/25625744/1187415), or simply specify the constant as 0x534E4150.

    Then Packet could be defined as

    struct Packet {
    
        let packetType : PacketType
    
        init(packetType : PacketType) {
            self.packetType = packetType
        }
    
        func data() -> NSData {
    
            let data = NSMutableData()
            data.appendInt32(0x534E4150) // 'SNAP'
            data.appendInt32(0)
            data.appendInt16(packetType.rawValue)
    
            return data
        }
    }
    

    and used as

    let pkt = Packet(packetType: .SignInRequest)
    println(pkt.data())
    // <534e4150 00000000 0064>