【问题标题】:Swift 1.2 subscript is unavailableSwift 1.2 下标不可用
【发布时间】:2015-04-11 14:12:10
【问题描述】:

在 Xcode 6.2 中完美运行的功能,现在我有一个错误'subscript' in unavailable: Indexing a String's UTF16View requires a String.UTF16View.Index, which can be constructed from Int when Foundation is imported。这是代码:

extension Character {
    var keyCode: Int {
        return Int(String(self).utf16[0])
    }
}

在这段代码中我得到了同样的错误:

extension NSEvent {
    var character: Int {
        return Int(charactersIgnoringModifiers!.utf16[0])
    }
}

【问题讨论】:

    标签: macos cocoa swift xcode6.3


    【解决方案1】:

    String.UTF16ViewCollectionType,因此您可以获取索引 通过使用其startIndex 属性来获取第一个元素:

    extension Character {
        var keyCode: Int {
            let utf16view = String(self).utf16
            return Int(utf16view[utf16view.startIndex])
        }
    }
    

    (我的电脑上没有 Xcode 6.2,因此我不能 解释为什么你的代码之前编译过。)

    【讨论】:

    • @NikitinRoman:这是一个完全不同的问题。请单独发布。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-30
    • 2016-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多