Невозможно добавить значение типа «[NSAttributedString.Key: Any]» с аргументом типа «String». - PullRequest
0 голосов
/ 21 сентября 2019

Я обновляю свое приложение для iOS 13 / Swift 5 и столкнулся с этой ошибкой.Я не уверен на 100%, как это исправить.Любые идеи?

@objc override open var tintColor: UIColor? {
    didSet {

        #if swift(>=4.2)
        var textAttributes = [NSAttributedString.Key : Any]()
        let foregroundColorKey = NSAttributedString.Key.foregroundColor
        #elseif swift(>=4)
        var textAttributes = [NSAttributedStringKey : Any]()
        let foregroundColorKey = NSAttributedStringKey.foregroundColor
        #else
        var textAttributes = [String:Any]()
        let foregroundColorKey = NSForegroundColorAttributeName
        #endif

        textAttributes[foregroundColorKey] = tintColor

        #if swift(>=4)

            if let attributes = convertFromOptionalNSAttributedStringKeyDictionary(titleTextAttributes(for: .normal)) {

                for (key, value) in attributes {
                    #if swift(>=4.2)
                    textAttributes[key] = value
                    #else
                    textAttributes[NSAttributedStringKey.init(key)] = value
                    #endif
                }
            }

        #else

            if let attributes = titleTextAttributes(for: .normal) {
                textAttributes = attributes
            }
        #endif

        setTitleTextAttributes(textAttributes, for: .normal)
    }
}

Ошибка его выбрасывания заключается в следующем:

Cannot subscript a value of type '[NSAttributedString.Key : Any]' with an argument of type 'String'

enter image description here

1 Ответ

0 голосов
/ 22 сентября 2019

Когда вы подписываете textAttributes внутри вашей версии Swift 4.2, вы используете key типа String.Вы хотите инициализировать NSAttributedString.Key, используя key перед подпиской textAttributes.Поэтому вместо:

textAttributes[key] = value

вы хотите сделать:

textAttributes[NSAttributedString.Key(key)] = value
...