Box Custom Struct in Objective-C [duplicate]
Asked Answered
P

1

7

Possible Duplicate:
How to wrap a Struct into NSObject
Can the new Clang Objective-C literals be redirected to custom classes?

I have a custom struct:

typedef struct {
    float f1;
    float f2;
} MYCustomStruct;

that I need to add to an NSArray. I've already written a category to create NSValues of these structs, which I then add to the NSArray, however I'd like to simplify that even further using boxed expressions, if possible. I'd love to be able to do this:

@[@(instanceOfMYCustomStruct)];

however, I'm confronted with the following error:

Illegal type 'MYCustomStruct' used in a boxed expression

Is there a way to use boxed expressions with custom structs?

Pabulum answered 29/10, 2012 at 16:23 Comment(3)
This question might answer your problem - #11252892Icbm
See #5692381Silversmith
Even if you could box it - how would you get it out? A simple cast wouldn't work, and you can't return an unknown type (unless it's a pointer) from a function in objc.Kittle
K
4

I would use a NSValue to box a struct, as it has built-in support for it. Unfortunately, you cannot use objective-c's cool literals for it, though:

struct foo myStruct;
NSValue *val = [NSValue valueWithBytes:&myStruct objCType:@encode(typeof(myStruct))];

// to pull it out...
struct foo myStruct;
[val getValue:&myStruct];

While this may be unwieldy & ugly amidst other objc code, you have to ask yourself - why are you using a struct in the first place in Objective-C? There are few speed performances gained over using an object with a few @property(s), the only real reason I could see is if you are integrating with a C library for compatibility with memory layouts, and even then, the structure of an objective-c object's memory layout is well-defined, so long as the superclass doesn't change.

So what is your real purpose in boxing a struct? If we have that, we can help you further.

Kittle answered 29/10, 2012 at 16:30 Comment(7)
If you use Objective-C++ you could define an overloaded Box() function that takes in your struct and returns an NSValue. Or just name the function B() for brevity and doesn't need more characters than Objective-C's boxing syntactic sugar.Strained
With Objective-C++ you could also define an operator NSValue*(const struct myStruct*) that converts it seamlessly.Strained
@Strained true, and in some situations that may be acceptable, but that does mean that the struct is no longer a POD.Kittle
The struct can still be a plain sequence of bytes -- just define the operator as a freestanding function (instead of a member function).Strained
There totally should be a NSValue boxing literal syntax on Clang. Maybe I'll write a patch...Folse
typedef struct __attribute__((objc_boxable)) { float f1; } myType; myType a; NSValue *val = @(a);Gethsemane
@Folse use __attribute__((objc_boxable))Gethsemane

© 2022 - 2024 — McMap. All rights reserved.