加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iOS Xcode Swift自动完成破坏了吗?

发布时间:2020-12-14 17:57:26 所属栏目:百科 来源:网络整理
导读:我没有使用 Swift那么多,但是来自Objective C,有一些关于Swift的东西,这是一个PITA来解决我的问题. 在iOS编程中,我们有animateWithDuration:方法,它是UIView的一部分. 所以我尝试使用Xcode的自动完成并开始输入: UIView.animateWith 自动填充显示: UIView
我没有使用 Swift那么多,但是来自Objective C,有一些关于Swift的东西,这是一个PITA来解决我的问题.

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

所以我尝试使用Xcode的自动完成并开始输入:

UIView.animateWith

自动填充显示:

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

然后我选中了“持续时间”字段,然后输入一个数字:

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

然后我再次选中了动画块,然后像在Objective C中一样按下输入,Xcode现在显示:

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

所以我最后一次用我的代码替换“代码”:

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

那时Xcode然后给我错误:

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

我不明白.这是Xcode为我生成的自动完成代码,为什么它会给我一个错误?

我注意到如果做一个简单的声明,如:

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

它没有给我任何错误.

任何人的想法?

解决方法

从 “Calling Methods Through Optional Chaining”开始:

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).如果一个闭包只包含一个表达式,
然后该表达式自动作为返回值.
错误信息是非常误导的,但它来自虚空的事实?是
与虚空不同.

添加显式返回语句可以解决问题:

UIView.animateWithDuration(0.5,0.0)
    return
})

更新:添加一个不必要的显式返回语句
再使用Swift 1.2(Xcode 6.3).从测试版发行说明:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读