繁体   English   中英

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

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

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

这给出了一个编译器错误,上面写着Type '[NSObject : AnyObject]!' does not conform to protocol 'DictionaryLiteralConvertible' Type '[NSObject : AnyObject]!' does not conform to protocol 'DictionaryLiteralConvertible' 我刚开始使用Swift并且无法弄清楚出了什么问题。

问题是NSUnderlineStyle.StyleSingle是一个Swift枚举值,它不符合AnyObject协议。 要解决这个问题,请调用toRaw()将其转换为Int

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

更新(适用于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