Convert Int to Uint32 in Swift

Convert Int to UInt32 in Swift

You can do it easily:

var x = UInt32(yourInt)

Swift converting between UInt and Int

In the first one, the return type is UInt, but you return Int since count returns Int.

Basically UInt has initializer which take variants of value types arguments such as Int, CGFloat, Double or event string and return a new value type.

  • UInt(8) // result is 8 UInt value type
  • UInt(20.12) // result is 20 UInt value type
  • UInt(Double(10)) // result is 10 UInt value type
  • UInt("10") // result is 10 UInt value type, note this is failable initializer, can be a value or nil

-

func numberOfPhotosInPhotoBrowser(photoBrowser: MWPhotoBrowser!) -> UInt {

return UInt(self.photos.count)
}

For the second one, the array subscript expects Int value where you are passing UInt, so create a new Int value type from UInt,

func photoBrowser(photoBrowser: MWPhotoBrowser!, photoAtIndex index: UInt) -> MWPhotoProtocol! {

return self.photos[Int(index)]
}

Swift convert UInt to Int

Int(arc4random_uniform(26)) does two things, one it eliminates the negative results from your current method and second should correctly creat an Int from the result.

Int to UInt (and vice versa) bit casting in Swift

You can do:

let unsigned = UInt8(bitPattern: Int8(-1)) // -> 255
let signed = Int8(bitPattern: UInt8(0xff)) // -> -1

Many similar initializers exist:

extension Int8 {
init(_ v: UInt8)
init(_ v: UInt16)
init(truncatingBitPattern: UInt16)
init(_ v: Int16)
init(truncatingBitPattern: Int16)
init(_ v: UInt32)
init(truncatingBitPattern: UInt32)
init(_ v: Int32)
init(truncatingBitPattern: Int32)
init(_ v: UInt64)
init(truncatingBitPattern: UInt64)
init(_ v: Int64)
init(truncatingBitPattern: Int64)
init(_ v: UInt)
init(truncatingBitPattern: UInt)
init(_ v: Int)
init(truncatingBitPattern: Int)
init(bitPattern: UInt8)
}

How can I convert Int32 to Int in Swift?

The error is your ? after valueForKey.

Int initializer doesnt accept optionals.

By doing myUnit.valueForKey(“theNUMBER”)?.intValue! gives you an optional value and the ! at the end doesnt help it.

Just replace with this:

return Int(myUnit.valueForKey(“theNUMBER”)!.intValue)

But you could also do like this if you want it to be fail safe:

return myUnit.valueForKey(“theNUMBER”)?.integerValue ?? 0

And to shorten you function you can do this:

func myNumber() -> Int {
let myUnit = self.getObject("EntityName") as! NSManagedObject

return myUnit.valueForKey("theNUMBER")?.integerValue ?? 0
}

How to convert UInt to Int in swift?

It's because it is optional, you need to unwrap it

var x: UInt?

if let z = x {
let y = Int(exactly: z)
}

Note that Int(exactly:) returns an optional as well so you might want to use a guard statement or another if let...

Update, as pointed out by vacawama Int(z) might crash

How to convert the byte data of Int32 to UInt32 and back?

Cast the variable via bitPattern (thanks Jthora). Plenty of help here on SO for this:

  • Converting signed to unsigned in Swift
  • Int to UInt (and vice versa) bit casting in Swift

For 32-bits, 0xffffffff=> -1 when signed or 4294967295 unsigned.

How to initialize optional UInt from optional Int?

You can map the optional:

var optionalNumber : Int?

//later in code
let optionalPositiveNumber = optionalNumber.map { UInt($0) }

From the documentation:

Evaluates the given closure when this Optional instance is not nil, passing the unwrapped value as a parameter.

func map<U>(_ transform: (Wrapped) throws -> U) rethrows -> U?

So the result is an UInt? as requested, and is either nil or the converted signed number.

Note that the conversion will fail (and crash with a runtime exception)
if the given number is negative. If that is an issue, a better variant
might be

let optionalPositiveNumber = optionalNumber.flatMap { UInt(exactly: $0) }

which returns nil if the given number is nil or negative.



Related Topics



Leave a reply



Submit