值是“ _type_type”是什么意思?

时间:2019-01-23 23:23:20

标签: swift

我已经多次遇到此错误,我不确定这是什么意思。我尝试过在线寻找解决方案,但没有一个专门解决.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等

1 个答案:

答案 0 :(得分:2)

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