Use of undeclared type 'AttributedString'
Asked Answered
T

1

8

Xcode 8 beta 4 no longer recognizes Foundation class AttributedString.

I've reproduced it in this simple playground example:

//: Playground - noun: a place where people can play

import Foundation

let attrStr1 = NSAttributedString()
let attrStr2 = AttributedString()  // Use of undeclared type 'AttributedString'

Since AttributedString was available in older Xcode 8 Swift 3 betas, I imagine this is a Foundation bug that needs to be fixed, rather than some source code error in Playground?

Transceiver answered 2/8, 2016 at 2:14 Comment(5)
I don't think it is a bug. I think they reversed the naming change from NSAttributeString->AttributedString back to use the prefix. Presumably to keep the NS prefix consistent in Foundation. Overall there are lots more naming changes in beta 4 than I expected. Most are obvious, some are trickier.Highwrought
@MattWeinecke and @PetahChristian: interesting reversal from Apple if true. I can't find any mention of from it from the Xcode 8 Beta 4 release notes though. I'd personally welcome back the NS prefix. As there are too many things to remember now: Data is a struct but NSData is a class; same with URL, etc.Pudens
I had thought it was weird for example that AttributedString had dropped the prefix but not NSMutableAttributedString.Highwrought
I have tested here and you need to use NSAttributedString. My project doesn't even compile with AttributedString.Predicant
Found the reason explained in SE-0086 @LeoDabus.Transceiver
T
10

Although undocumented in the Xcode release notes, a version 2 update to Swift evolution proposal SE-0086 Drop NS Prefix in Swift Foundation has added the "NS" prefix back to several Foundation classes which previously dropped the prefix.

The reason is as follows:

If the class is planned to have a value-type equivalent in the near future, then keep the NS prefix. Examples: NSAttributedString, NSRegularExpression, NSPredicate.

So, the Swift AttributedString type will return at some point, as a struct next time, instead of being a class.

It sounds like some of these improvements will be "a focus area for Swift 4." For now, it's necessary to revert back to using the NSAttributedString class.

If you're curious to know how many types were affected by the SE-0086 v2 update, it looks like the revision affects ~32 types which had previously dropped the NS prefix for Swift 3.

Transceiver answered 2/8, 2016 at 14:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.