Swift 4.1: Type 'String' has no member 'foregroundColor'

I have the below line of code in a cocoa pod which I am compiling in Swift 4.1 on Xcode.

let foregroundColor = attributedTitle(for: .normal)?.attribute(.foregroundColor, at: 0, effectiveRange: nil) as? UIColor

The error I am getting for the line is:

Type 'String' has no member 'foregroundColor'

There is a similar discussion here Getting an error: Type 'String' has no member 'foregroundColor' in Swift 4 but what confuses me is that this answer suggests the error I am seeing is in fact one to do with Swift 3.X whereas I am compiling in Swift 4.1.

Also, when I try to edit the line it is blocked (as it is from a cocoa pod) which makes me wonder if there isn't another way of soling it (as messing with the pods will cause a problem every time I update in the future)?

1 answer

  • answered 2018-08-15 22:36 Alejandro Viquez

    i hope attributedTitle(for: .normal) is a fuction who return a String value, but your code is prepared to recive an attributedString like this:

    let attributeTitle = NSAttributedString(string: "StringExample")
    let color = attributeTitle.attribute(.foregroundColor, at: 0, effectiveRange: nil) as? UIColor

    the problem i hope is the function attributedTitle, search him in the pod and try tu change the return value or check if an update in the library is uploaded.