Closed josephjohnston closed 1 month ago
I assume you closed this because you figured out the problem by yourself, but for future reference, I'll give my two cents on this: What you're doing should be possible as just values.to_vec_retained()
, and will (probably) eventually be fixed by https://github.com/madsmtm/objc2/issues/563.
But while it's not, the easiest fix is probably to use the code from to_vec_retained
directly, with Value
plugged in as the T
:
let values: Id<NSArray<Value>> = msg_send_id![self, values];
let v: Vec<Id<Value>> = values.to_vec().into_iter().map(|obj| unsafe {
Id::retain(obj as *const Value as *mut Value).unwrap_unchecked()
}).collect();
Thanks, yes I realized I just needed to parameterize NSArray with Value
When I model some Objective-C object type
Value
I'm confused on the best way to receive anNSArray
of these objects and convert toVec<Id<Value>>
.Here I model some fictional Objective-C type
Value
asThen calling method
values
withself
some instance of structValue
I receive anNSArray
of these valuesThe issue is
x
is of typeId<AnyObject>
rather thanId<Value>
because I typedvalues
asId<NSArray>
. I can't typevalues
asId<NSArray<Value>>
becauseValue
doesn't implement theClassType
trait. I model many types as above and never have to deal with classes, and I hope to keep it that way. The following fails for the same reasonIn
0.3.0-beta.5
the following worked fineI see you took away
into_vec
for lack of safety because the array isIsIdCloneable
so may be cloned even when containing mutable elements. When I try theinto_vec
code you used, that iswhere
T=Value
here, this fails becauseto_vec
converts to a vector of&AnyObject
and then casting&AnyObject
to*const T
is an invalid cast. So I'm really not sure at this point how to properly convert an array to a vector when the type of objects in the array don't implementClassType
.