Is it possible to create something like a C struct for Objective-C? I need to be able to use it in an NSArray so it cannot be a traditional struct. Right now I am declaring a whole class just to accomplish this and I was wondering if there is a simpler way.
What I currently have:
@interface TextureFile : NSObject
@property NSString *name;
@property GLKTextureInfo *info;
@end
@implementation TextureFile
@synthesize name = _name;
@synthesize info = _info;
@end
NSMutableArray *textures;
What I want to do:
typedef struct {
NSString *name;
GLKTextureInfo *info;
} TextureFile;
NSMutable array *textures;
It depends what kind of data you’re using, the example you are using in your question seems okay for a struct.
If you need to store a C struct in an
NSArray, which requires an object, you can convert the C-struct toNSValueand store it like that, you then convert back to its C struct type when you read it.Check the Apple Documentation.
Given this struct:
To store it:
To read it again: