简体   繁体   English

输入'[NSObject:AnyObject]!' 不符合协议'DictionaryLiteralConvertible'

[英]Type '[NSObject : AnyObject]!' does not conform to protocol 'DictionaryLiteralConvertible'

self.textView.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.blackColor(),
                                    NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle]

This gives a compiler error saying Type '[NSObject : AnyObject]!' does not conform to protocol 'DictionaryLiteralConvertible' 这给出了一个编译器错误,上面写着Type '[NSObject : AnyObject]!' does not conform to protocol 'DictionaryLiteralConvertible' Type '[NSObject : AnyObject]!' does not conform to protocol 'DictionaryLiteralConvertible' . Type '[NSObject : AnyObject]!' does not conform to protocol 'DictionaryLiteralConvertible' I've just started Swift and cannot figure out what's wrong. 我刚开始使用Swift并且无法弄清楚出了什么问题。

The problem is that NSUnderlineStyle.StyleSingle is a Swift enum value and it doesn't conform to the AnyObject protocol. 问题是NSUnderlineStyle.StyleSingle是一个Swift枚举值,它不符合AnyObject协议。 To get around this, call toRaw() to convert it to an Int : 要解决这个问题,请调用toRaw()将其转换为Int

self.textView.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.blackColor(),
                                    NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.toRaw()]

Update (for Xcode 6.1): 更新(适用于Xcode 6.1):

The function toRaw() has been replaced with the property rawValue in Xcode 6.1. 函数toRaw()已被Xcode 6.1中的属性rawValue替换。

self.textView.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.blackColor(),
                                    NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM