相关疑难解决方法(0)

Swift参数中的默认关键字

在读取初始值设定项时,NSLocalizedString我看到某些参数默认为某个值default.什么是default关键词代表什么?

func NSLocalizedString(key: String, tableName: String? = default, bundle: NSBundle = default, value: String = default, #comment: String) -> String
Run Code Online (Sandbox Code Playgroud)

ios swift

53
推荐指数
1
解决办法
6179
查看次数

难以将默认值分配给函数的参数

在一个类中,我定义了一个私有常量,我尝试使用常量作为函数参数的默认值:

class Foo {
  // instance variable
  private let DefaultValue = 10

  // Compiler error: Cannot use instance member 'DefaultValue' as a default parameter
  public func doTask(amount: Int = DefaultValue) {
    ...
  }
}
Run Code Online (Sandbox Code Playgroud)

但我收到编译器错误:Cannot use instance member 'DefaultValue' as a default parameter.

然后,我也尝试声明DefaultValueprivate static:

class Foo {
      // static variable
      private static let DefaultValue = 10

      // Compiler error: Static let 'DefaultValue' is private and cannot be referenced from a default argument value …
Run Code Online (Sandbox Code Playgroud)

swift swift4

8
推荐指数
1
解决办法
767
查看次数

标签 统计

swift ×2

ios ×1

swift4 ×1