iOS Xcode Swift 自动完成功能坏了? [英] iOS Xcode Swift autocomplete broken?
问题描述
我没怎么用过 Swift,但我来自 Objective C,有一些关于 Swift 的东西是 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.
所以我尝试使用 Xcode 的自动完成并开始输入:
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)
然后我再次切换到动画块,然后像我通常在 Objective C 中所做的那样按下 Enter,Xcode 现在显示:
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
})
所以我最后一次用我的代码替换代码":
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);
})
那时 Xcode 给了我错误:
That's when Xcode then gives me the error:
无法使用类型参数列表调用animateWithDuration"'(FloatLiteralConvertible, 动画: () -> Void)'
Cannot invoke 'animateWithDuration' with an argument list of type '(FloatLiteralConvertible, animations: () -> Void)'
我不明白.这是 Xcode 为我生成的自动完成代码,为什么它给我一个错误?
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;
})
它没有给我任何错误.
大家有什么想法吗?
推荐答案
来自 "通过可选链调用方法":
任何通过可选链设置属性的尝试都会返回一个Void?
类型的值,它使您可以与 nil 进行比较以查看如果属性设置成功...
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 ...
因此表达式的类型
self.customView?.transform = CGAffineTransformMakeTranslation(0.0, 0.0)
是Void?
(可选的Void).如果一个闭包只包含一个单个表达式,那么这个表达式会自动作为返回值.错误信息相当具有误导性,但它来自于 Void?
是不同于Void
.
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
.
添加一个显式的return
语句即可解决问题:
Adding an explicit return
statement solves the problem:
UIView.animateWithDuration(0.5, animations: { () -> Void in
self.customView?.transform = CGAffineTransformMakeTranslation(0.0, 0.0)
return
})
更新: 添加一个明确的 return
语句是不必要的不再使用 Swift 1.2 (Xcode 6.3).来自 Beta 版发行说明:
Update: Adding an explicit return
statement it not necessary
anymore with Swift 1.2 (Xcode 6.3). From the beta release notes:
具有非 Void 返回类型的未注释的单表达式闭包现在可以在 Void 上下文中使用.
Unannotated single-expression closures with non-Void return types can now be used in Void contexts.
这篇关于iOS Xcode Swift 自动完成功能坏了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!