iOS的X $ C $ç雨燕自动完成破? [英] iOS Xcode Swift autocomplete broken?

查看:115
本文介绍了iOS的X $ C $ç雨燕自动完成破?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有使用雨燕的多,而是因为目标C的到来,有大约斯威夫特的几件事情,这是一个PITA左右让我的头。

I haven't used Swift that much but coming from Objective C, there's a few things about Swift that's a PITA to get my head around.

在iOS的编程中,我们有 animateWithDuration:方法,这是UIView的一部分

In iOS programming, we have animateWithDuration: method, which is part of UIView.

于是,我就使用的是X code的自动完成,并开始键入:

So I tried to use Xcode's autocomplete and begin to type:

UIView.animateWith

自动完成所示:

UIView.animateWithDuration(duration: NSTimeInterval, animations: () -> Void)

我然后在标签的持续时间字段,然后键入的一个号码:

I then tabbed across to the "duration" field, and then typed in a number:

UIView.animateWithDuration(0.5, animations: () -> Void)

然后我整个标签再次向动画块,和pressed像我通常如何做到这一点在Objective C的输入,X code现在显示:

Then I tabbed across again to the animation block, and pressed enter like how I normally do it in Objective C, Xcode now shows:

UIView.animateWithDuration(0.5, animations: { () -> Void in
    code
})

于是我标签最后一次与我的code来代替code:

So then I tabbed one last time to replace "code" with my code:

UIView.animateWithDuration(0.5, animations: { () -> Void in
    self.customView?.transform = CGAffineTransformMakeTranslation(0.0, 0.0);
})

这是当X code,然后给我的错误:

That's when Xcode then gives me the error:

不能援引animateWithDuration'类型的参数列表
  (FloatLiteralConvertible,动画:() - >无效)

Cannot invoke 'animateWithDuration' with an argument list of type '(FloatLiteralConvertible, animations: () -> Void)'

我不明白。这是自动完成code对我产生的X code,为什么它给我的错误?

I don't understand. That's the autocomplete code that Xcode generated for me, why is it giving me an error ?

我注意到,如果做一个简单的语句,如:

I noticed if do a simple statement like:

UIView.animateWithDuration(0.5, animations: { () -> Void in
    var num = 1 + 1;
})

它并没有给我任何错误。

It doesn't give me any errors.

任何想法吗?

推荐答案

从<一个href=\"https://developer.apple.com/library/ios/documentation/swift/conceptual/Swift_Programming_Language/OptionalChaining.html#//apple_ref/doc/uid/TP40014097-CH21-XID_373\"相对=nofollow>调用通过可选的链接方法:

任何试图通过可选的链接来设置属性返回
  类型的值无效?,它使您能够比较的零看
  如果属性设置成功...

Any attempt to set a property through optional chaining returns a value of type Void?, which enables you to compare against nil to see if the property was set successfully ...

因此​​,前pression类型

Therefore the type of the expression

self.customView?.transform = CGAffineTransformMakeTranslation(0.0, 0.0)

无效?(可选无效)。如果一个封闭只包含一个的单前pression 的,
那么这个前pression被自动取为的返回值的。
该错误信息是非常容易引起误解,但它来自于一个事实,即无效?
虚空

is Void? (optional Void). And if a closure consists only of a single expression, then this expression is automatically taken as the return value. The error message is quite misleading, but it comes from the fact that Void? is different from Void.

添加一个明确的收益语句解决了这个问题:

Adding an explicit return statement solves the problem:

UIView.animateWithDuration(0.5, animations: { () -> Void in
    self.customView?.transform = CGAffineTransformMakeTranslation(0.0, 0.0)
    return
})

更新:添加一个明确的收益语句就没有必要
再和雨燕1.2(X code 6.3)。从公测版本说明:

Update: Adding an explicit return statement it not necessary anymore with Swift 1.2 (Xcode 6.3). From the beta release notes:

未注解单前pression与非空的返回类型封闭,现在可以在虚空的环境中使用。

Unannotated single-expression closures with non-Void return types can now be used in Void contexts.

这篇关于iOS的X $ C $ç雨燕自动完成破?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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