2015-10-04 131 views
2

我的代码在雨燕1.2工作得很好,但在斯威夫特2我收到此错误:错误:“无法将类型为String:UIColor?的值分配给类型为String:AnyObject的值!”

Cannot assign a value of type String: UIColor? to a value of type String: AnyObject!

我的代码是:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath) as! JSQMessagesCollectionViewCell 

    let message = messages[indexPath.row] 

    if message.senderId == self.senderId { 
     cell.textView!.textColor = UIColor.blackColor() 
    } else { 
     cell.textView!.textColor = UIColor.whiteColor() 
    } 

    // Error in the following line 
    cell.textView!.linkTextAttributes = [NSForegroundColorAttributeName:cell.textView!.textColor] 

    return cell 
} 

回答

2

您必须添加一个!textColor

cell.textView!.linkTextAttributes = [NSForegroundColorAttributeName:cell.textView!.textColor!] 

原因是linkTextAttributes[String : AnyObject!]而字典你想要分配的类型是[String : UIColor?]UIColorAnyObject之间的不匹配,因为UIColor可以投射到AnyObject。问题是您提供的UIColor?是可选的。而且,您不能将可选UIColor?隐式转换为非可选AnyObject!。因此you必须强制展开颜色才能通过添加!UIColor?获得UIColor

+0

感谢的人!它的工作:)。对不起,我不能投票回答你的答案,因为我是新来的,我没有足够的声望(至少需要15个)。 –

+0

你可以投我的问题,我会得到10个声望(我认为),我已经有5个 –

0

尝试用这种

textView!.linkTextAttributes = [NSForegroundColorAttributeName:textView!.textColor as AnyObject!] 
相关问题