objective c - How to write Int32 to NSOutputStream in Swift -


I am trying to write int32 on an NSOutputStream in Swift and I'm having trouble in ObjC, I did something like that Would:

  - (zero) write4ByteField: (int32_t) value {[write stream: ((value> 24) & amp; 0xff)]; [Write stream: ((value> gt; 16) & amp; 0xff)]; [Write stream: ((value> 8) & amp; 0xff)]; [Write stream: (value & amp; 0xff)]; }  

However, in Swift, it does not really like to make all my lower-level bit-shifting and I left it to cast values ​​everywhere.

>

But I get an error is not convertible to $ T4 for

  func write4ByteField (value: Int32) {data = NSDTA (bytes: & amp; Value, length: size (int32) stream.write (data.bytes, maxLength: sizeof (Int32)} any suggestions? I'm guessing I'm wrong about it Way.   

& Amp; value < / Code>, and data.bytes need to be cast:

  func write4ByteField (var value: int32) {data = NSDTA (bytes: & amp; Value, length: size (int32)) write stream (unsafe indicator (data. Byte), maxlength: sizeof (int32)}}  

this NSData Can also be done without, which includes a conversion in the Big-endian byte sequence:

  func write4ByteField (value: Int32) {var valueBE = value.bigEndian withUnsafePointer (& amp; ValueBE) {self.stream.write (unsafe indicator ($ 0), maximum length: sizeofValue (valueBE)}}  

Comments