How to Convert Int32 to Int in Swift

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
}

Swift 3 - convert Int32 to Int

You can try this.

let number1: Int32 = 10
let number2 = Int(number1)

Convert Int to UInt32 in Swift

You can do it easily:

var x = UInt32(yourInt)

How to convert Int32 to String in Swift 4?

The question isn't clear but what I think this issue boils down to is that you can't init a string with an optional value.

So either do as @matt suggested and force unwrap the cartItem

String(cartItem!.quantity)

or supply a default value

String(cartItem?.quantity ?? 0)

Of course if you need to handle the fact that you might not have a cart then it is better to do it like

if let cart = cartItem {
let str = "\(cart.quantity)" //or String(cart.quantity)
//do stuff with str
} else {
//handle no cart state
}

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)]
}

how to convert Int32 value to CGFloat in swift?

To convert between numerical data types create a new instance of the target type, passing the source value as parameter. So to convert an Int32 to a CGFloat:

let int: Int32 = 10
let cgfloat = CGFloat(int)

In your case you can either do:

let width = CGFloat(CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!).width)
let height = CGFloat(CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!).height)

myLayer?.frame = CGRectMake(0, 0, width, height)

or:

let width = CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!).width
let height = CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription as CMVideoFormatDescriptionRef!).height

myLayer?.frame = CGRectMake(0, 0, CGFloat(width), CGFloat(height))

Note that there is no implicit or explicit type casting between numeric types in swift, so you have to use the same pattern also for converting a Int to Int32 or to UInt etc.

how to convert Data to Int in swift

first convert your data into string like blow and then use that string to initialize int

let stringInt = String.init(data: yourdata, encoding: String.Encoding.utf8)
let int = Int.init(stringInt ?? "")

this will return an optional value which you can unwrap to use further



Related Topics



Leave a reply



Submit