toNSData method

NSData toNSData()

Return a NSData containing the contents of the List interpreted as bytes.

The elements of the List should be integers in the range 0 to 255. Any integer, which is not in that range, is converted to a byte as if by value.toUnsigned(8).

Implementation

NSData toNSData() {
  if (length == 0) {
    return NSData();
  }
  final buffer = malloc<Uint8>(length);
  buffer.asTypedList(length).setAll(0, this);

  final nsData = NSData.dataWithBytes(buffer.cast(), length: length);
  malloc.free(buffer);

  return nsData;
}