Swft UInt->Int 或者 Int->UInt



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?

share | improve this question
 
 
If these are your own methods, you should just be using Int instead of UInt. Even though the values are never going to be negative, the recommendation from the Swift documentation is essentially to always use Int unless you have a specific reason to do otherwise (interoperability with C functions, for example). –  Nate Cook  Sep 28 at 16:44
 
These are from MWPhotoBrowser (ObjectiveC). I've used Bridging Headers to let me use it but its a bit quirky this way. I've no choice but to use the UInt or fork out the MWPhotoBrowser and edit it (bit harder to maintain)–   Akshat  Sep 28 at 16:52

2 Answers

up vote 2 down vote accepted

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

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 typecast UInt to Int,

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

    return self.photos[Int(index)]
}
share | improve this answer
 
 
Awesome!, I kept trying to use as and :UInt and stuff –   Akshat  Sep 28 at 15:46
 
This is probably OK in this case, but you have to be careful because converting an Int to UInt in this way will crash if the Int is negative. It is safer to use UInt(bitPattern: myint). And vice versa, converting a UInt to an Int will crash if the UInt is too big for the int. It is safer to use Int(bitPattern: myuint). –   vacawama  Sep 28 at 15:50
 
That is not a type cast, you are creating a new Int/UInt. In this example it makes no real difference though. –  Viktor Lexington  Sep 28 at 16:30

// 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`


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?

share | improve this question
 
    
If these are your own methods, you should just be using Int instead of UInt. Even though the values are never going to be negative, the recommendation from the Swift documentation is essentially to always use Int unless you have a specific reason to do otherwise (interoperability with C functions, for example). –  Nate Cook  Sep 28 at 16:44
    
These are from MWPhotoBrowser (ObjectiveC). I've used Bridging Headers to let me use it but its a bit quirky this way. I've no choice but to use the UInt or fork out the MWPhotoBrowser and edit it (bit harder to maintain)–   Akshat  Sep 28 at 16:52

2 Answers

up vote 2 down vote accepted

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

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 typecast UInt to Int,

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

    return self.photos[Int(index)]
}
share | improve this answer
 
    
Awesome!, I kept trying to use as and :UInt and stuff –   Akshat  Sep 28 at 15:46
    
This is probably OK in this case, but you have to be careful because converting an Int to UInt in this way will crash if the Int is negative. It is safer to use UInt(bitPattern: myint). And vice versa, converting a UInt to an Int will crash if the UInt is too big for the int. It is safer to use Int(bitPattern: myuint). –   vacawama  Sep 28 at 15:50
    
That is not a type cast, you are creating a new Int/UInt. In this example it makes no real difference though. –  Viktor Lexington  Sep 28 at 16:30

// 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`

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值