【问题标题】:How to pass NSDateComponent as a function parameter如何将 NSDateComponent 作为函数参数传递
【发布时间】:2018-10-28 20:53:30
【问题描述】:

如何将 NSDateComponent 对象或引用作为函数参数(只读)传递?以下代码在“编译”时失败,并出现“使用未声明的类型 'NSDateComponent'”错误:

import Foundation
class PreventativeCare {
    var birthday = NSDateComponents()
    func setPatientDOB(birthdate:NSDateComponent /* compile error */ ) {
        birthday.day = birthdate.day
        birthday.month = birthdate.month
        birthday.year = birthdate.year
    }
}

【问题讨论】:

    标签: swift function parameter-passing


    【解决方案1】:

    将 NSDateComponent 更正为 NSDateComponents 并且效果很好。

    【讨论】:

    • 请注意,您应该使用 Swift 对象 DateComponents,而不是 Objective-C 版本 NSDateComponents
    【解决方案2】:

    看起来你刚刚错过了NSDateComponents 中的s :) 很容易犯错误,特别是因为 Cocoa 中的复数类型很少见。

    【讨论】:

      猜你喜欢
      • 2014-06-29
      • 1970-01-01
      • 2021-01-14
      • 2016-07-23
      • 2021-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-13
      相关资源
      最近更新 更多