Search code examples
swiftswift4.2nsmutableattributedstring

NSMutableAttributedString: Ambiguous use of 'init(string:attributes:)'


I am trying to run the code I wrote some months ago. Now I use Swift 4.2/Xcode 10.1. I get the error Ambiguous use of 'init(string:attributes:)' on

let mutableAttributedString = NSMutableAttributedString(string: "", attributes: [:])

I also use SwiftyAttributes 4.3.0. Here I found public convenience init(string str: String, attributes: [Attribute]), but I cannot understand why Swift wants to call this function.

How can I solve this issue? Is it necessary to update SwiftyAttributes?


Solution

  • The problem was in the init method declared in SwiftyAttributes 4.3.0:

    extension NSAttributedString {
        public convenience init(string str: String, attributes: [Attribute]) {
            self.init(string: str, attributes: dictionary(from: attributes))
        }
    }
    

    In SwiftyAttributes 5.0.0 this method was renamed to public convenience init(string str: String, swiftyAttributes attrs: [Attribute]). Therefore, I updated SwiftyAttributes to solve the problem. See this link for more detail about the fix added to SwiftyAttributes 5.0.0.