|= in Swift 5 ?

Using copyfile's flags, I have an example in what I assume is C but no idea what it translates to in Swift ... "flags: Int32 = COPY_ALL" but want to append "flags |= COPY_MOVE" ?

Replies

Usually, flags in Swift are defined as option sets.

such as:

let singleOption: ShippingOptions = .priority
let multipleOptions: ShippingOptions = [.nextDay, .secondDay, .priority]
let noOptions: ShippingOptions = []


Have a look here:

https://developer.apple.com/documentation/swift/optionset

or

https://stackoverflow.com/questions/14295469/what-does-mean-pipe-equal-operator


In your case, I suspect

flags: Int32 = COPY_ALL

and

flags |= COPY_MOVE

will result in

flags = COPY_ALL

Just had another test and "flags |= COPYFILE_..." does work but for some reason COPYFILE_MOVE is causing a problem.

Which problem is it causing ?

It depends on many things if such flags opration would work or not. And please be more precise when asking something.

You are asking `COPY_ALL` or `COPY_MOVE` ? Or `COPYFILE_...` or `COPYFILE_MOVE` ?

What are those `flags` for? What value does it contain when you write `COPYFILE_MOVE` ?


for some reason COPYFILE_MOVE is causing a problem

What sort of problem? Please describe it.

This is an artefact of the way that the C declarations are being imported into Swift. The C declaration for

copyfile_flags_t
is:
typedef uint32_t copyfile_flags_t;

which is imported as this:

public typealias copyfile_flags_t = UInt32

That is, your flags are essentially a

UInt32
. In contrast, the
COPYFILE_MOVE
flag is declared in C as this:
#define COPYFILE_MOVE (1<<20)

which is imported as this:

public var COPYFILE_MOVE: Int32 { get }

giving it a type of

Int32
. Clearly this is less than ideal, and I encourage you to file a bug against
<copyfile.h>
requesting that it’s declarations be made more Swift friendly. Please post your bug number, just for the record.

In the meantime, the best workaround is to convert the flags to the correct sign using

UInt32(bitPattern:)
. For example:
var flags: copyfile_flags_t = 0
flags |= UInt32(bitPattern: COPYFILE_MOVE)

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

If I choose "COPYFILE_ALL | COPYFILE_RECURSIVE | COPYFILE_MOVE" it won't work when the destination is a root folder.