I have the following methods
var photos = [MWPhoto] = [MWPhoto]()
func numberOfPhotosInPhotoBrowser(photoBrowser: MWPhotoBrowser!) -> UInt {
return self.photos.count
}
func photoBrowser(photoBrowser: MWPhotoBrowser!, photoAtIndex index: UInt) -> MWPhotoProtocol! {
return self.photos[index]
}
However for the first I get Int is not convertible to UInt
(since self.photos.count
is an Int
and for the second UInt is not convertible to Int
- since the self.photos[
can only take an Int for its index.
How can I correctly convert the UInt to Int and back?
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.
-
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)]
}
// initializing Int
var someInt: Int = 8
someInt
// Converting Int to UInt
var someIntToUInt: UInt = UInt(someInt)
someIntToUInt
// initializing UInt
var someUInt: UInt = 10
someUInt
// Converting UInt to Int
var someUIntToInt: Int = Int(someUInt)
someUIntToInt
If you want unsigned int from negative value use UInt(bitPattern:)
let intVal = -1
let uintVal = UInt(bitPattern: intVal) // uintVal == 0xffffffffffffffff
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With