How to disable Sendable check to a setter?

The AttributedString is Sendable, but AttributedString.paragraphStyle is NSParagraphStyle, which is not Sendable, so this code in Swift 5.9 will give me this error:

var attributeString = AttributedString("")
attributeString.paragraphStyle = NSParagraphStyle() // Conformance of 'NSParagraphStyle' to 'Sendable' is unavailable

I raise a feedback since Xcode 15 b1, but it still happen in b6, it seems Apple doesn't care about this, so is there any solution to disable Sendable check to a setter?

1 Like

I would love a proper solution, but this works as an awkward workaround that doesn't produce a warning:

var attributeString = AttributedString("")
attributeString.mergeAttributes(.init([.paragraphStyle: style]))

You can use extension NSParagraphStyle: @unchecked Sendable {} in your code. Technically it may be unsafe due to NSParagraph style being a class with a mutable subclass but as long as you don't mutate it directly it should be fine.

1 Like

The warning in Xcode says that NSParagraphStyle has been explicitly marked as not Sendable. To you're point I'm assuming it's only because of the mutable subclass, but it still seems sketchy to retroactively conform a type to Sendable that has explicitly marked itself as not Sendable.

Although to be fair I'm sure my suggestion is also just skirting around an intended requirement of AttributedString.

This works! Thanks!

Oddly enough, Xcode 15b8 doesn't produce that warning, despite the fact that it's still explicitly unavailable in the module definition. It seems to be effective since the warning about NSParagraphStyle not being Sendable goes away. This seems like a bug.

1 Like

I'm seeing this warning in Xcode 15.2 and 15.3. I created a new project and added this method to get a minimal repro:

func test() {
  var container = AttributeContainer()
  let paragraphStyle = NSMutableParagraphStyle()
  paragraphStyle.lineHeightMultiple = 0.5
  container.paragraphStyle = paragraphStyle
}

On both versions I tested, I'm getting "Conformance of 'NSParagraphStyle' to 'Sendable' is unavailable"

Try it like this instead:

let container = AttributeContainer([.paragraphStyle: paragraphStyle])

It may not be any more safe, but it eliminates the warning :wink:

1 Like

Thank you! I was having a hard time figuring out the solution just from reading the AttributeContainer documentation.