【问题标题】:Cannot convert value of type 'NSLocale' to type 'Locale' in coercion while changing calendar Georgian to Persian在将日历格鲁吉亚语更改为波斯语时,无法强制将“NSLocale”类型的值转换为“Locale”类型
【发布时间】:2018-07-20 06:02:28
【问题描述】:

我正在尝试将 Georgian Calendar 转换为 Persian Calendar 使用:

dateAndTimePicker.calendar = NSCalendar(identifier: NSCalendar.Identifier.persian)! as Calendar!
dateAndTimePicker.locale = NSLocale(localeIdentifier: "fa_IR") as Locale

日历会相应地转换,但在更改日历和时间选择器的语言时在第 2 行出现错误:

修改后:

dateAndTimePicker.locale = NSLocale(localeIdentifier: "fa_IR") as Locale

到:

dateAndTimePicker.locale = Locale.init(Identifier: "fa_IR")

出现此错误:

对于datepicker,我做了一个outlet:

@IBOutlet weak var dateAndTimePicker: UIDatePicker!

【问题讨论】:

  • dateAndTimePicker.locale = Locale.init(Identifier: "fa_IR") 中将Identifier 替换为identifier
  • 大小写很重要:identifier,而不是Identifier
  • 不,在这种情况下不会。也试过了。
  • 当然情况很重要。 Swift 是一种区分大小写的语言。这是Locale(identifier: "fa_IR"),不是Locale(Identifier: "fa_IR")

标签: ios swift datepicker calendar


【解决方案1】:

你不需要使用NSLocale现在你可以替换

dateAndTimePicker.locale = NSLocale(localeIdentifier: "fa_IR") as Locale

dateAndTimePicker.locale = Locale.init(identifier: "fa_IR")

【讨论】:

  • 现在它给出了这个错误:参数传递给不带参数的调用
  • 最新的。 9.4.1.
  • 已更新。请检查并指导。
【解决方案2】:

您无需创建NSCalendar。请创建 swift 变量并按如下方式使用

    let calendar = Calendar.init(identifier: Calendar.Identifier.persian)!
    let locale = Locale.init(identifier: "fa_IR")
    dateAndTimePicker.calendar = calendar
    dateAndTimePicker.locale = locale

【讨论】:

  • 现在它给出了这个错误:参数传递给不带参数的调用
  • 你的“dateAndTimePicker”变量是什么?显示它的声明
  • 请按照我的回答更新日历和语言环境,然后进行构建。不要使用 NSCalender 或 NSLocale。替换为我的回答并构建。
  • 嘿,我发现你的语法有错误...检查标识符的拼写...你写了标识符所以发生错误
  • 没有 Rakesh 不是拼写问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-25
  • 1970-01-01
相关资源
最近更新 更多