更好的“无功能”在Swift中? [英] Better "nothing function" in Swift?
问题描述
我有这个结构,
typealias Tweak =(
抓住:(_ p:SomeClass)-> ; CGFloat,
更改:(_ p:SomeClass,_ n:CGFloat)->(),
from:CGFloat,up to:CGFloat
)
(因此,第一行为您提供了一个值,第二行更改了某些内容,最后两个只是限制。) / p>
所以,您可能会有很多这样的事情……
var调整:[Tweak] = [
({_ in 0},{_ in},0,0),
({p in mainHeight},
{ p,n in
mainHeight = n
paintDisplayWhatever()},
8.0,28.0),
({p in Some.global},
{p,n in
Some.global = n
fireball.adjust(heat:n)},
8.0,28.0),
等等
我的问题...
注意第一个数组,我只是希望它成为无版本的调整
所以,我做到了
没有调整:调整=({_ in 0},{_ in},0,0)
在Swift中,有没有更好的方法来完成这两个什么都不做的闭包,或者确实有一种更正确的方法来完成整个事情?
没什么调整:调整= lessNilThanNil
没有内置值表示闭包返回0 CGFloat
value,更不用说其中的一个元组以及其他两个零值了。
但是如果您 create a类型,例如 struct
表示 Tweak
(而不是使用 typealias < /元组的$code>),您可以定义
static
常量来表示 Tweak $的默认值c $ c>。例如:
class SomeClass {}
struct Tweak {
//随时将我重命名为更合适的
static let zero = Tweak(grab:{_ in 0},change:{_ in},from:0,up to:0)
var抓住:(_ p:SomeClass)-> CGFloat
var change:(_ p:SomeClass,_ n:CGFloat)->无效
变量,来自:CGFloat
变量直到:CGFloat
}
现在无论何时需要进行调整
,您都可以说 .zero
来引用您的零默认值(此正是 CGPoint
这样的类型):
var调整:[调整] = [
.zero,
调整(
抓取:{p in 25},
更改:{p,n in
print(p,n)
},
from:8,up:28
),
Tweak(
抓住:{p in 27},
更改:{p,n in
print(p,n * 4)
},
从:8,最多:28
)
]
创建结构比创建 typealias <更可取在这种情况下为元组的/ code>。元组实际上只是设计为临时类型,不能用于长期存储。结构很多更强大并且适合长期使用。
I have this structure,
typealias Tweak = (
grab:(_ p: SomeClass)->CGFloat,
change:(_ p: SomeClass, _ n: CGFloat)->(),
from:CGFloat, upto:CGFloat
)
(So, the first line "gives you a value", the second "changes something", the last two are just limits.)
So, you might have an array of such things ...
var tweaks:[Tweak] = [
({_ in 0}, {_ in}, 0,0),
( {p in mainHeight},
{p, n in
mainHeight = n
paintDisplayWhatever()},
8.0, 28.0),
( {p in Some.global},
{p, n in
Some.global = n
fireball.adjust(heat: n)},
8.0, 28.0),
etc
My question ...
notice the first one in the array, I simply wanted it to be the "nothing" version of a Tweak
So, I did this
nothingTweak: Tweak = ({_ in 0}, {_ in}, 0,0)
In Swift is there a better way to do the two "nothing" closures, or indeed, a more correct way to do the whole thing?
nothingTweak: Tweak = lessNilThanNil
you know?
There's no built-in value that represents "a closure which returns a 0 CGFloat
value", let alone a tuple of them along with two other zero values.
However if you create a type, such as a struct
, to represent a Tweak
(rather than using a typealias
of a tuple), you can define static
constant(s) to represent "default" values of a Tweak
. For example:
class SomeClass {}
struct Tweak {
// feel free to rename me to something more appropriate
static let zero = Tweak(grab: {_ in 0 }, change: {_ in }, from: 0, upto: 0)
var grab: (_ p: SomeClass) -> CGFloat
var change: (_ p: SomeClass, _ n: CGFloat) -> Void
var from: CGFloat
var upto: CGFloat
}
Now wherever a Tweak
is expected, you can just say .zero
to refer to your "zero default" (this is exactly what types like CGPoint
do):
var tweaks: [Tweak] = [
.zero,
Tweak(
grab: { p in 25 },
change: { p, n in
print(p, n)
},
from: 8, upto: 28
),
Tweak(
grab: { p in 27 },
change: { p, n in
print(p, n * 4)
},
from: 8, upto: 28
)
]
Creating a structure is much more preferable to creating a typealias
of a tuple in this case. Tuples are only really designed to be transient types, not to be used for long-term storage. Structures are much more powerful and suited for long-term use.
这篇关于更好的“无功能”在Swift中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!