【问题标题】:Getting "missing argument label" error trying to call Swift method [duplicate]尝试调用 Swift 方法时出现“缺少参数标签”错误 [重复]
【发布时间】:2017-07-23 02:06:19
【问题描述】:

我是 Swift 新手。我的简单功能有问题。它不工作,Xcode Playground 报错:

调用 print(hello("txt")) 中缺少参数标签 'name:'

代码如下:

func hello(name:String)->String{
    return name
}
print(hello("txt"))

我怎样才能解决这个问题以使其正常工作?

【问题讨论】:

  • print(hello(name: "Your string")) 像这样调用函数。它将解决您的问题。
  • 您是否尝试过应用 Xcode 建议的修复?
  • 在发布问题之前请search on an error
  • @Nirmalsinh 谢谢
  • @Hamish 也是 X-code 的新手! :))

标签: swift function


【解决方案1】:

您在调用 print(hello("txt")) 时缺少参数标签 'name:'
试试这个方法

func hello(name:String)->String{
   return name
}
print(hello(name : "txt"))

您应该阅读语言指南。 https://developer.apple.com/.../Swift.../TheBasics.html

【讨论】:

  • 谢谢。但是,你不认为 swift 与 c 和 c++ 相比是一个弱点吗?这种调用函数的方式毫无意义!
  • @Arman 这在什么方面没有意义?参数标签可以为调用站点添加大量信息。不看文档,你能告诉我调用CGContextAddArc(ctx, 0, 100, 5, 0, M_PI, 1) 的每个参数是做什么的吗?当然,如果给定参数的作用很明显,则您不必总是需要参数标签。您可以在必要时使用_ 省略它们。
  • @Hamish 我的意思是根据我在 C++ 方面的经验,这对我来说很奇怪
【解决方案2】:

您缺少一个参数标签,这就是原因:

你可以像下面这样调用

print(hello(name : "txt"))

或者你可以忽略带有前缀“_”的参数。

func hello(_ name:String)->String{
   return name
}
print(hello("txt"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    • 1970-01-01
    • 2022-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多