views:

76

answers:

2

I have a struct in ClassA and want to assign it to the same struct in ClassB (the two structs are the same with different names). ClassA and ClassB are view controllers. Basically, I need to pass this struct to another class. However, structs don't seem to be exposed as members, which means I can't access them. Here is ClassA's struct declared in its header file:

typedef struct {
NSString *startDate;
NSString *endDate;
NSString *classId;
}  selectedItemsClassAStruct;

selectedItemsClassAStruct selectedItemsClassA;

and the same for ClassB (just with ClassA text replaced)

It doesn't appear in code hints for ClassB. I see this error if I try to access it:

request for member 'selectedItemsClassBStruct' in something not a structure or union

How should it be done?

+1  A: 

What you want to do is called re-interpreting cast or type punning; which means changing one structure into another structure that have the same members.

This trick is used by Cocoa to convert a NSRect into a CGRect for example.

I recommend the reading of a good post on the subject written by Matt Gallagher.

Laurent Etiemble
A: 

Matt Gallagher describes how to (safely) implement properties for structs in this post.

Matt G