How to add my custom struct to a NSArray ?
12/07/03 22:39
I try to [myArray addObject:&myStruct] and it doesn't work…
NSArray, NSDictionary stores only non-nil Objective-C object references ! You have to wrap your structure in a class :
@interface MyClass : NSObject
{
MyStructType _myStruct ;
...
}
+ (id)newMyClass:my_needed_parameters_here...;
- (id)initMyClass:my_needed_parameters_here...;
...
@end
Then you will add object of class MyClass to the NSArray instance.
If you need to insert nil references, replace them by [NSNull null]. Later, when you will retrieve objects from the NSArray or the NSDictionary instance, you can test if the object is of class NSNull with isKindOfClass or isMemberOfClass methods.
@interface MyClass : NSObject
{
MyStructType _myStruct ;
...
}
+ (id)newMyClass:my_needed_parameters_here...;
- (id)initMyClass:my_needed_parameters_here...;
...
@end
Then you will add object of class MyClass to the NSArray instance.
If you need to insert nil references, replace them by [NSNull null]. Later, when you will retrieve objects from the NSArray or the NSDictionary instance, you can test if the object is of class NSNull with isKindOfClass or isMemberOfClass methods.