Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crashes with custom NSAttributedStringKey #25

Closed
scalessec opened this issue Apr 22, 2018 · 3 comments
Closed

Crashes with custom NSAttributedStringKey #25

scalessec opened this issue Apr 22, 2018 · 3 comments

Comments

@scalessec
Copy link

When working with NSAttributedStrings that use custom NSAttributedStringKey values, SwiftyAttributes crashes whenever attributes are accessed.

Here's one way to reproduce:

let attributedString = NSMutableAttributedString(string: "hello world")
attributedString.setAttributes([NSAttributedStringKey("crash"): "bang"], range: NSRange(..<attributedString.length))
attributedString.enumerateSwiftyAttributes(in: 0..<attributedString.length) { attributes, _, _ in
    debugPrint(attributes)
}

I realize that type-safety for a finite number of Foundation values is part of SwiftyAttributes's appeal, but I also think this crash should be handled by the library due to the existence of NSAttributedStringKey(string:).

I haven't given too much thought into how I think this could be handled, but perhaps something like:
.custom([String: Any]) is an appropriate solution for any NSAttributedStringKey values that can't be constructed via NSAttributedStringKey(rawValue:).

FWIW, this came up due to some work I'm doing with markdown rendering, where we're storing the URL for links in a custom attribute as part of the attributed string.

@eddiekaiger
Copy link
Owner

Interesting, I've never considered custom string keys. I like the idea of a .custom attribute, although I feel like .custom(String, Any) would make more sense to ensure a one-to-one mapping of attribute to Attribute. Started working on it already!

@eddiekaiger
Copy link
Owner

@scalessec Just opened #26, feel free to give feedback!

@eddiekaiger
Copy link
Owner

Fixed in v4.2.0! Thanks for calling this out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants