diff --git a/src/swift/Data.swift b/src/swift/Data.swift index d51205ce7..e0647e743 100644 --- a/src/swift/Data.swift +++ b/src/swift/Data.swift @@ -50,7 +50,9 @@ public struct DispatchData : RandomAccessCollection { /// /// - parameter bytes: A pointer to the memory. It will be copied. public init(bytes buffer: UnsafeBufferPointer) { - let d = dispatch_data_create(buffer.baseAddress!, buffer.count, nil, _dispatch_data_destructor_default()) + let d = buffer.baseAddress == nil ? _swift_dispatch_data_empty() + : dispatch_data_create(buffer.baseAddress!, buffer.count, nil, + _dispatch_data_destructor_default()) self.init(data: d) } @@ -60,7 +62,8 @@ public struct DispatchData : RandomAccessCollection { /// - parameter deallocator: Specifies the mechanism to free the indicated buffer. public init(bytesNoCopy bytes: UnsafeBufferPointer, deallocator: Deallocator = .free) { let (q, b) = deallocator._deallocator - let d = dispatch_data_create(bytes.baseAddress!, bytes.count, q?.__wrapped, b) + let d = bytes.baseAddress == nil ? _swift_dispatch_data_empty() + : dispatch_data_create(bytes.baseAddress!, bytes.count, q?.__wrapped, b) self.init(data: d) }