相关疑难解决方法(0)

为什么我不能实例化一个嵌套类的空数组?

我似乎在使用[foo]()样式语法实例化嵌套类类型的空数组时出现问题:

// Playground - noun: a place where people can play

class outsideClass {

}

class Wrapper {
    class InsideClass {
    }
}

var foo = [outsideClass]() // Works fine

// Invalid use of '()' to call a value of non-function type '[Wrapper.InsideClass.Type]'
var bar = [Wrapper.InsideClass]() 
Run Code Online (Sandbox Code Playgroud)

这是我误解的东西 - 它是在我的咖啡之前,但我已经检查了发行说明,我认为你应该能够引用这样的嵌套类 - 或者是beta 7中的错误?

这可以作为一种解决方法:

var foobar: [Wrapper.InsideClass] = []
Run Code Online (Sandbox Code Playgroud)

swift

22
推荐指数
1
解决办法
1506
查看次数

覆盖功能

我正在一所着名大学提供的在线iOS课程.我不明白为什么以下代码使用override它是合法的.

  1. 根据官方定义,我们使用override覆盖超类的方法.以下代码中的子类和超类在哪里?

  2. 什么被覆盖,什么?

public override var description: String {
    return "\(url.absoluteString) (aspect ratio = \(aspectRatio))"
}
Run Code Online (Sandbox Code Playgroud)

ios swift

10
推荐指数
1
解决办法
1万
查看次数

无法在Swift 3的Collection扩展中使用indices.contains()

我在Swift 2.3中写了以下扩展:

extension CollectionType {
    /// Returns the element at the specified index iff it is within bounds, otherwise nil.
    subscript (safe index: Index) -> Generator.Element? {
        return indices.contains(index) ? self[index] : nil
    }
}
Run Code Online (Sandbox Code Playgroud)

但事实证明,Swift 3.0没有contains()功能.相反,它为我提供了以下语法:

indices.contains(where: { (<#Self.Indices.Iterator.Element#>) -> Bool in
    <# code ??? what should it do??? #>
})
Run Code Online (Sandbox Code Playgroud)

问题是我不知道它在块内应该包含什么.请帮忙迁移它吗?

swift swift3 swift2.3

10
推荐指数
1
解决办法
2365
查看次数

标签 统计

swift ×3

ios ×1

swift2.3 ×1

swift3 ×1