无法使用类型的参数列表调用NSAttributedString类型的初始值设定项(字符串:字符串,属性:[NSObject:AnyObject]?)

时间:2015-10-12 12:49:46

标签: ios xcode swift swift2

我将代码从XCode 6.4迁移到7,这个错误似乎无法解决。

func onHoldReasonAttributedTextSetup(onHoldReson : String) -> NSMutableAttributedString
{

    let markerFontAttributes : [NSObject : AnyObject]? = [
      NSFontAttributeName : UIFont(name: "HelveticaNeue", size: 12.0)!,
      NSForegroundColorAttributeName : UIColor(red: 0/255, green: 152/255, blue: 204/255, alpha: 1.0)
    ]
    let onHoldText = NSAttributedString(string: "On Hold: ", attributes: markerFontAttributes)
    let onHoldReasonText  = NSAttributedString(string:onHoldReson)
    var string = NSMutableAttributedString()
    string.appendAttributedString(onHoldText)
    string.appendAttributedString(onHoldReasonText)
    return string
}

1 个答案:

答案 0 :(得分:1)

您需要更改此行:

let markerFontAttributes : [NSObject : AnyObject]? = [
      NSFontAttributeName : UIFont(name: "HelveticaNeue", size: 12.0)!,
      NSForegroundColorAttributeName : UIColor(red: 0/255, green: 152/255, blue: 204/255, alpha: 1.0)
    ]

let markerFontAttributes : [String : AnyObject]? = [
      NSFontAttributeName : UIFont(name: "HelveticaNeue", size: 12.0)!,
      NSForegroundColorAttributeName : UIColor(red: 0/255, green: 152/255, blue: 204/255, alpha: 1.0)
    ]

因为初始化程序需要[String : AnyObject]?格式的字典:

init(string str: String, attributes attrs: [String : AnyObject]?)

参考:NSAttributedString Class Reference