Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 是否向函数发送默认参数值?_Swift_Default Arguments - Fatal编程技术网

Swift 是否向函数发送默认参数值?

Swift 是否向函数发送默认参数值?,swift,default-arguments,Swift,Default Arguments,TL;博士 如果我有func show(message:String=“Hello”),如何在不省略参数名的情况下将默认参数发送给它?(例如,显示(消息:默认)) 注意:show()不是我要找的!详情见下文 假设我们定义了以下函数: func makeCreature(color: UIColor, eyeCount: Int = 2, noseCount: Int = 1) -> Creature { // ... } 然后我们还定义了另一个方法,makebiotes: func

TL;博士

如果我有
func show(message:String=“Hello”)
,如何在不省略参数名的情况下将默认参数发送给它?(例如,
显示(消息:默认)

注意:
show()
不是我要找的!详情见下文


假设我们定义了以下函数:

func makeCreature(color: UIColor, eyeCount: Int = 2, noseCount: Int = 1) -> Creature {
  // ...
}
然后我们还定义了另一个方法,
makebiotes

func makeCreatures(count: Int, color: UIColor) {
  for 1...count {
    makeCreature(color: color)
  }
}
然而,现在我们想要为
makebiotes
轻松定制眼计数和鼻计数。一种方法是重新定义参数及其默认值:

解决方案#1

func makeCreatures(count: Int, color: UIColor, eyeCount: Int = 2, noseCount: Int = 1) {
  for 1...count {
    makeCreature(color: color, eyeCount: eyeCount, noseCount: noseCount)
  }
}
问题是,如果默认的眼睛数量发生变化,我需要记住在两个位置更新它:
makebioters
makebioters

我希望做的是将该方法定义为:

func makeCreatures(count: Int, color: UIColor, eyeCount: Int? = nil, noseCount: Int? = nil)
但是,这意味着我现在必须创建4个不同的
if
分支:

解决方案#2

func makeCreatures(count: Int, color: UIColor, eyeCount: Int? = nil, noseCount: Int? = nil) {
  for 1...count {
    if let eyeCount = eyeCount, let noseCount = noseCount {
      makeCreature(color: color, eyeCount: eyeCount, noseCount: noseCount) 
    } else if let eyeCount = eyeCount {
      makeCreature(color: color, eyeCount: eyeCount)
    } else if let noseCount = noseCount {
      makeCreature(color: color, noseCount: noseCount)
    } else {
      makeCreature(color: color)
    }
  }
}
必须创建4个不同的分支有点难看,很难理解。有没有更好的方法可以让我将溶液1的简洁性与溶液2的干燥性结合起来?类似于此:

理想溶液?

func makeCreatures(count: Int, color: UIColor, eyeCount: Int? = nil, noseCount: Int? = nil) {
  for 1...count {
    makeCreature(color: color, 
      eyeCount: eyeCount ?? default, 
      noseCount: noseCount ?? default)
  }
}
其中
default
表示使用
makebiote
中定义的默认参数值(即
2
用于
eyecont
1
用于
noscont


如果没有,还有哪些解决方案可以帮助我实现这一目标?

这并不完全是您想要的,但您可以使用builder模式来实现这一点:

struct Creature {
    let color: UIColor
    let eyeCount: Int
    let noseCount: Int
}

struct CreatureBuilder {
    var color: UIColor?
    var eyeCount: Int = 2 //store defaults here
    var noseCount: Int = 1

    func build() -> Creature {
        guard let color = color else { fatalError("Creatures need a color!") }

        return Creature(color: color, eyeCount: eyeCount, noseCount: noseCount)
    }
}

func makeCreatures(count: Int, color: UIColor, eyeCount: Int? = nil, noseCount: Int? = nil) {
    var builder = CreatureBuilder()
    builder.color = color;
    if let eyeCount = eyeCount { builder.eyeCount = eyeCount } //override defaults only for non-nil params
    if let noseCount = noseCount { builder.noseCount = noseCount }

    for _ in 1...count {
        let creature = builder.build() //TODO: do something with the creature.
    }
}

为了完整性起见,还有另一个类似的解决方案

你可以创建一个结构来保存生物的属性:

struct Attributes {
  let color: UIColor
  let eyeCount: Int = 2
  let noseCount: Int = 1
}
然后重新定义要接受属性的函数:

func makeCreature(attributes: Attributes) -> Creature {
  // ...
}

func makeCreatures(count: Int, attributes: Attributes) {
  for 1...count {
    makeCreature(color: color, attributes: attributes)
  }
}
允许您在两个函数中使用默认值:

// uses 2 eyes (default), and 2 noses
makeCreature(attributes: Attributes(color: .purple, noseCount: 2))

// use 10 eyes, and 1 nose (default)
makeCreatures(count: 3, attributes: Attributes(color: .blue, eyeCount: 10))

只需在两个函数都可用的某个范围内设置两个Int常量,并使它们成为函数的默认参数。也不需要选项。我很好奇为什么你的
makebiotel
不是
biotel
的初始值设定项。