Soft Dev Soft Dev - 25 days ago 8
Swift Question

Binary operator '/' cannot be applied to two 'UInt32' operands

Here is my code.

var frameCount = INT_MAX
let totalSize: UInt32 = 4096
let itemSize: UInt32 = 64
frameCount = totalSize / itemSize


I get "Binary operator '/' cannot be applied to two 'UInt32' operands" error message.
Is it really impossible or what did I miss?

Answer

The error message is a bit misleading. INT_MAX is defined as

public var INT_MAX: Int32 { get }

so with

var frameCount = INT_MAX

frameCount is defined as a variable of type Int32. The result of the division totalSize / itemSize is a UInt32 however, and Swift does not implicitly convert types.

You can fix that by changing the initial definition to

var frameCount = UINT32_MAX

or perhaps simpler, let the compiler infer the type:

let totalSize: UInt32 = 4096
let itemSize: UInt32 = 64
let frameCount = totalSize / itemSize

If you need the result as a signed integer then you have to convert it explicitly, e.g.

let frameCount = Int32(totalSize / itemSize)
Comments