【问题标题】:What does it mean for a value to be of "_type_type"?值是“_type_type”是什么意思?
【发布时间】:2019-01-24 07:29:01
【问题描述】:

我已经多次遇到这个错误,但我不确定它是什么意思。我试图在网上寻找解决方案,但没有一个专门解决 .type 的问题。

这是我当前的错误:

private func setUpCache() {
    let urlCache: URLCache = URLCache(memoryCapacity: MEMORY_CAPACITY, diskCapacity: DISK_CAPACITY, diskPath: "myDiskPath")
    URLCache.shared = URLCache //<-Cannot assign value of type 'URLCache.type' to type 'URLCache'
}

我已经看到这个错误也发生在其他类型上。和 Int 成为 Int.type 或 String 成为 string.type 等意味着什么

【问题讨论】:

  • 您需要分配URLCache 的实例,而不是URLCache 本身的类。
  • 这是最近的变化吗?其他在线示例(例如:stackoverflow.com/questions/38249573/…)似乎表明这很好。谢谢你的回答!
  • 那些例子并没有做你在自己的问题中所做的事情。

标签: swift


【解决方案1】:

URLCache 是类型(它是一个类)。 URLCache.shared 需要分配一个类的实例。但是您正在尝试分配类本身(类型为URLCache.type)。

你的坏话应该是:

URLCache.shared = urlCache // your urlCache variable, not the URLCache type

每当您看到关于 SomeType.type 的类似消息时,这意味着您正在尝试分配实际类型而不是该类型的实例。

let num: Int = Int // Cannot convert value of type 'Int.Type' to specified type 'Int'

当然应该是:

let num: Int = 42 // Assign an actual Int value or variable of type Int

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-10
    • 1970-01-01
    • 2011-08-12
    • 2017-06-11
    • 2018-03-05
    • 2023-03-27
    • 2013-03-09
    相关资源
    最近更新 更多