猿问

Xcode 7.3已弃用“ ++”和“-”运算符

我正在查看Xcode 7.3注释,并且注意到了这个问题。


++和-运算符已被弃用


有人可以解释为什么不推荐使用它吗?我说对了,现在在新版本的Xcode中,您将使用它代替++它x += 1;


例:


for var index = 0; index < 3; index += 1 {

    print("index is \(index)")

}


子衿沉夜
浏览 631回答 3
3回答

白衣染霜花

我意识到,尽管如此,此评论仍未解决问题,可能有人会在寻找一种解决方案,以使这些操作员保持工作状态,这种解决方案可以在底部找到。?我个人更喜欢++和--运营商。我不同意它们棘手或难以管理的观点。一旦开发人员了解了这些运算符的功能(并且我们正在谈论非常简单的内容),则代码应该非常清楚。在解释为什么不推荐使用运算符时,提到了它们的主要用途是C风格的for循环。我对其他人一无所知,但我个人根本不使用C风格的循环,在还有其他地方或情况下,++或--运算符是有用的。我还要提及的是,它varName++返回一个值,因此可以在returnwhile中使用它,而varName += 1不能在其中使用。对于任何想让这些操作员在这里工作的人,解决方案是:prefix operator ++ {}postfix operator ++ {}prefix operator -- {}postfix operator -- {}// Incrementprefix func ++(inout x: Int) -> Int {&nbsp; &nbsp; x += 1&nbsp; &nbsp; return x}postfix func ++(inout x: Int) -> Int {&nbsp; &nbsp; x += 1&nbsp; &nbsp; return (x - 1)}prefix func ++(inout x: UInt) -> UInt {&nbsp; &nbsp; x += 1&nbsp; &nbsp; return x}postfix func ++(inout x: UInt) -> UInt {&nbsp; &nbsp; x += 1&nbsp; &nbsp; return (x - 1)}prefix func ++(inout x: Int8) -> Int8 {&nbsp; &nbsp; x += 1&nbsp; &nbsp; return x}postfix func ++(inout x: Int8) -> Int8 {&nbsp; &nbsp; x += 1&nbsp; &nbsp; return (x - 1)}prefix func ++(inout x: UInt8) -> UInt8 {&nbsp; &nbsp; x += 1&nbsp; &nbsp; return x}postfix func ++(inout x: UInt8) -> UInt8 {&nbsp; &nbsp; x += 1&nbsp; &nbsp; return (x - 1)}prefix func ++(inout x: Int16) -> Int16 {&nbsp; &nbsp; x += 1&nbsp; &nbsp; return x}postfix func ++(inout x: Int16) -> Int16 {&nbsp; &nbsp; x += 1&nbsp; &nbsp; return (x - 1)}prefix func ++(inout x: UInt16) -> UInt16 {&nbsp; &nbsp; x += 1&nbsp; &nbsp; return x}postfix func ++(inout x: UInt16) -> UInt16 {&nbsp; &nbsp; x += 1&nbsp; &nbsp; return (x - 1)}prefix func ++(inout x: Int32) -> Int32 {&nbsp; &nbsp; x += 1&nbsp; &nbsp; return x}postfix func ++(inout x: Int32) -> Int32 {&nbsp; &nbsp; x += 1&nbsp; &nbsp; return (x - 1)}prefix func ++(inout x: UInt32) -> UInt32 {&nbsp; &nbsp; x += 1&nbsp; &nbsp; return x}postfix func ++(inout x: UInt32) -> UInt32 {&nbsp; &nbsp; x += 1&nbsp; &nbsp; return (x - 1)}prefix func ++(inout x: Int64) -> Int64 {&nbsp; &nbsp; x += 1&nbsp; &nbsp; return x}postfix func ++(inout x: Int64) -> Int64 {&nbsp; &nbsp; x += 1&nbsp; &nbsp; return (x - 1)}prefix func ++(inout x: UInt64) -> UInt64 {&nbsp; &nbsp; x += 1&nbsp; &nbsp; return x}postfix func ++(inout x: UInt64) -> UInt64 {&nbsp; &nbsp; x += 1&nbsp; &nbsp; return (x - 1)}prefix func ++(inout x: Double) -> Double {&nbsp; &nbsp; x += 1&nbsp; &nbsp; return x}postfix func ++(inout x: Double) -> Double {&nbsp; &nbsp; x += 1&nbsp; &nbsp; return (x - 1)}prefix func ++(inout x: Float) -> Float {&nbsp; &nbsp; x += 1&nbsp; &nbsp; return x}postfix func ++(inout x: Float) -> Float {&nbsp; &nbsp; x += 1&nbsp; &nbsp; return (x - 1)}prefix func ++(inout x: Float80) -> Float80 {&nbsp; &nbsp; x += 1&nbsp; &nbsp; return x}postfix func ++(inout x: Float80) -> Float80 {&nbsp; &nbsp; x += 1&nbsp; &nbsp; return (x - 1)}prefix func ++<T : _Incrementable>(inout i: T) -> T {&nbsp; &nbsp; i = i.successor()&nbsp; &nbsp; return i}postfix func ++<T : _Incrementable>(inout i: T) -> T {&nbsp; &nbsp; let y = i&nbsp; &nbsp; i = i.successor()&nbsp; &nbsp; return y}// Decrementprefix func --(inout x: Int) -> Int {&nbsp; &nbsp; x -= 1&nbsp; &nbsp; return x}postfix func --(inout x: Int) -> Int {&nbsp; &nbsp; x -= 1&nbsp; &nbsp; return (x + 1)}prefix func --(inout x: UInt) -> UInt {&nbsp; &nbsp; x -= 1&nbsp; &nbsp; return x}postfix func --(inout x: UInt) -> UInt {&nbsp; &nbsp; x -= 1&nbsp; &nbsp; return (x + 1)}prefix func --(inout x: Int8) -> Int8 {&nbsp; &nbsp; x -= 1&nbsp; &nbsp; return x}postfix func --(inout x: Int8) -> Int8 {&nbsp; &nbsp; x -= 1&nbsp; &nbsp; return (x + 1)}prefix func --(inout x: UInt8) -> UInt8 {&nbsp; &nbsp; x -= 1&nbsp; &nbsp; return x}postfix func --(inout x: UInt8) -> UInt8 {&nbsp; &nbsp; x -= 1&nbsp; &nbsp; return (x + 1)}prefix func --(inout x: Int16) -> Int16 {&nbsp; &nbsp; x -= 1&nbsp; &nbsp; return x}postfix func --(inout x: Int16) -> Int16 {&nbsp; &nbsp; x -= 1&nbsp; &nbsp; return (x + 1)}prefix func --(inout x: UInt16) -> UInt16 {&nbsp; &nbsp; x -= 1&nbsp; &nbsp; return x}postfix func --(inout x: UInt16) -> UInt16 {&nbsp; &nbsp; x -= 1&nbsp; &nbsp; return (x + 1)}prefix func --(inout x: Int32) -> Int32 {&nbsp; &nbsp; x -= 1&nbsp; &nbsp; return x}postfix func --(inout x: Int32) -> Int32 {&nbsp; &nbsp; x -= 1&nbsp; &nbsp; return (x + 1)}prefix func --(inout x: UInt32) -> UInt32 {&nbsp; &nbsp; x -= 1&nbsp; &nbsp; return x}postfix func --(inout x: UInt32) -> UInt32 {&nbsp; &nbsp; x -= 1&nbsp; &nbsp; return (x + 1)}prefix func --(inout x: Int64) -> Int64 {&nbsp; &nbsp; x -= 1&nbsp; &nbsp; return x}postfix func --(inout x: Int64) -> Int64 {&nbsp; &nbsp; x -= 1&nbsp; &nbsp; return (x + 1)}prefix func --(inout x: UInt64) -> UInt64 {&nbsp; &nbsp; x -= 1&nbsp; &nbsp; return x}postfix func --(inout x: UInt64) -> UInt64 {&nbsp; &nbsp; x -= 1&nbsp; &nbsp; return (x + 1)}prefix func --(inout x: Double) -> Double {&nbsp; &nbsp; x -= 1&nbsp; &nbsp; return x}postfix func --(inout x: Double) -> Double {&nbsp; &nbsp; x -= 1&nbsp; &nbsp; return (x + 1)}prefix func --(inout x: Float) -> Float {&nbsp; &nbsp; x -= 1&nbsp; &nbsp; return x}postfix func --(inout x: Float) -> Float {&nbsp; &nbsp; x -= 1&nbsp; &nbsp; return (x + 1)}prefix func --(inout x: Float80) -> Float80 {&nbsp; &nbsp; x -= 1&nbsp; &nbsp; return x}postfix func --(inout x: Float80) -> Float80 {&nbsp; &nbsp; x -= 1&nbsp; &nbsp; return (x + 1)}prefix func --<T : BidirectionalIndexType>(inout i: T) -> T {&nbsp; &nbsp; i = i.predecessor()&nbsp; &nbsp; return i}postfix func --<T : BidirectionalIndexType>(inout i: T) -> T {&nbsp; &nbsp; let y = i&nbsp; &nbsp; i = i.predecessor()&nbsp; &nbsp; return y}
随时随地看视频慕课网APP
我要回答