madsmtm / objc2

Bindings to Apple's frameworks in Rust
https://docs.rs/objc2/
MIT License
281 stars 35 forks source link

NSAttributedString::initWithString_attributes has the wrong type &NSDictionary<NSString> #605

Closed eks5115 closed 3 weeks ago

eks5115 commented 3 weeks ago
let dict = NSDictionary::from_vec(
    &[&*NSString::from_str("NSForegroundColorAttributeName")],
    vec![NSColor::whiteColor()]
);
let attr_title = NSAttributedString::initWithString_attributes(NSAttributedString::alloc(), &title, Some(dict.as_ref()));
button.setAttributedTitle(&attr_title);
42 let a_title = NSAttributedString::initWithString_attributes(NSAttributedString::alloc(), &title, Some(dict.as_ref())); ---- ^^^^^^^^^^^^^ expected &NSDictionary<NSString>, found &NSDictionary<NSString, NSColor>
arguments to this enum variant are incorrect
madsmtm commented 3 weeks ago

You have to downcast the NSColor to AnyObject before passing it to the function:

vec![Id::as_super(Id::as_super(NSColor::whiteColor()))]
madsmtm commented 3 weeks ago

Which, I know, isn't pretty, but that's how it works currently

eks5115 commented 3 weeks ago

Thank you! Compile successfully.

let title = NSString::from_str("title");
let dict = NSDictionary::from_vec(
    &[NSForegroundColorAttributeName],
    vec![Id::into_super(Id::into_super(NSColor::redColor()))]
);
let attr_title = NSAttributedString::initWithString_attributes(NSAttributedString::alloc(), &title, Some(dict.as_ref()));