Swift:使用不同的整数大小时出错 [英] Swift: Errors when using different integer sizes

查看:55
本文介绍了Swift:使用不同的整数大小时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试Swift,因为这显然是Apple希望我们进军的方向。
但是,我似乎对您似乎无法添加不同整数的事实感到非常恼火大小:

I've been trying out Swift, since it's obviously the direction that Apple wants us to go in. However, I've been really annoyed with the fact that you can't seem to add integers of different sizes:

    var a: Int64 = 1500
    var b: Int32 = 12349

    var c = a + b
    if a < b { ... }

产生的错误是找不到'+'的重载接受提供的参数-显然是因为它们是对象类型。所有类方法似乎都不会对上/下转换整数有所帮助。

The yielded error is "Could not find an overload for '+' that accepts the supplied argument' — obviously since they are object types. None of the class methods seem to be of any help in up/down-converting integers.

同样的情况也适用显然,带有任何类型的别名(CInt + CLong)。

Same situation applies with any of the type aliases, obviously, (CInt + CLong).

我可以看到很多现实世界中能够做到的现实情况

I can see a lot of real-world situations where it is immensely practical to be able to do integer arithmetic let alone comparisons or bitwise operations on two disparately-sized integers.

如何解决这个问题?使用 as 运算符进行显式转换似乎没用。Swift语言书也没有太大帮助,因为它没有真正讨论这种情况。

How to solve this? Explicit casting with the as operator doesn't seem to work. The Swift language book isn't much help either as it doesn't really discuss this scenario.

推荐答案

let a: Int64 = 1500
let b: Int32 = 12349
let c = a + Int64(b)
println("The value of c is \(c)")

这篇关于Swift:使用不同的整数大小时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆