In Obj-C, a common practice was to use convenience functions to perform common operations, like configuring auto layout for views:
在Obj-C中,常见的做法是使用便捷函数来执行常见操作,例如为视图配置自动布局:
func makeConstraint(withAnotherView : UIView) -> NSLayoutConstraint
{
// Make some constraint
// ...
// Return the created constraint
return NSLayoutConstraint()
}
If you just needed to set the constraint and forget about it, you could call:
如果您只需要设置约束并忘记它,您可以调用:
[view1 makeConstraint: view2]
[view1 makeConstraint:view2]
If you wanted to store the constraint later so that you could remove/modify it, you would do something like:
如果您想稍后存储约束以便可以删除/修改它,您可以执行以下操作:
NSLayoutConstraint * c;
c = [view1 makeConstraint: view2]
I want to do this in swift, but if I call the above function and do not capture the returned constraint, I get the warning:
我想在swift中执行此操作,但如果我调用上面的函数并且不捕获返回的约束,我会收到警告:
Result of call to 'makeConstraint(withAnotherView:)' is unused
VERY annoying. Is there some way to let Swift know that I don't always want to capture the return value?
很烦人。有没有办法让Swift知道我并不总是想要捕获返回值?
NOTE: I know about this. It is ugly and not what I'm looking for:
注意:我知道这一点。这很难看,而不是我想要的:
_ = view1.makeConstraint(withAnotherView: view2)
2 个解决方案
#1
25
This is behaviour that has been introduced in Swift 3. Instead of having to explicitly annotate functions with @warn_unused_result
in order to tell the compiler that the result should be used by the caller, this is now the default behaviour.
这是在Swift 3中引入的行为。为了告诉编译器调用者应该使用结果,不必使用@warn_unused_result显式地注释函数,现在这是默认行为。
You can use the @discardableResult
attribute on your function in order to inform the compiler that the return value doesn't have to be 'consumed' by the caller.
您可以在函数上使用@discardableResult属性,以通知编译器返回值不必由调用者“消耗”。
@discardableResult
func makeConstraint(withAnotherView : UIView) -> NSLayoutConstraint {
... // do things that have side effects
return NSLayoutConstraint()
}
view1.makeConstraint(view2) // No warning
let constraint = view1.makeConstraint(view2) // Works as expected
You can read about this change in more detail on the evolution proposal.
您可以在进化建议中更详细地了解此更改。
#2
0
You can try to turn the warnings off from the Build Settings of your project. I like the question, good point. Done some research and found this. https://*.com/a/34932152/5615274
您可以尝试从项目的“生成设置”中关闭警告。我喜欢这个问题,好点。做了一些研究,发现了这一点。 https://*.com/a/34932152/5615274
Did not test it yet.
还没测试过。
#1
25
This is behaviour that has been introduced in Swift 3. Instead of having to explicitly annotate functions with @warn_unused_result
in order to tell the compiler that the result should be used by the caller, this is now the default behaviour.
这是在Swift 3中引入的行为。为了告诉编译器调用者应该使用结果,不必使用@warn_unused_result显式地注释函数,现在这是默认行为。
You can use the @discardableResult
attribute on your function in order to inform the compiler that the return value doesn't have to be 'consumed' by the caller.
您可以在函数上使用@discardableResult属性,以通知编译器返回值不必由调用者“消耗”。
@discardableResult
func makeConstraint(withAnotherView : UIView) -> NSLayoutConstraint {
... // do things that have side effects
return NSLayoutConstraint()
}
view1.makeConstraint(view2) // No warning
let constraint = view1.makeConstraint(view2) // Works as expected
You can read about this change in more detail on the evolution proposal.
您可以在进化建议中更详细地了解此更改。
#2
0
You can try to turn the warnings off from the Build Settings of your project. I like the question, good point. Done some research and found this. https://*.com/a/34932152/5615274
您可以尝试从项目的“生成设置”中关闭警告。我喜欢这个问题,好点。做了一些研究,发现了这一点。 https://*.com/a/34932152/5615274
Did not test it yet.
还没测试过。