Re: Adding Structure in NSArray
Re: Adding Structure in NSArray
- Subject: Re: Adding Structure in NSArray
- From: Fredrik Olsson <email@hidden>
- Date: Fri, 18 Aug 2006 09:30:28 +0200
omer riaz skrev:
Hi
I have a small problem. I wanted convert dynamic linklist, which is
composed of struct, into NSArray. Is there any method, I can take this
struct as a unit of NSArray. I have tried the following code but it is
not working. It gives me error at id songs
As said before, you can only put NSObject and descendants into NSArray
(or other the container classes). So if you want to store a struct, you
need to wrap it in a descendant of NSObject. A NSData could do the trick.
I would make a category on NSData to help me, perhaps like this:
@interface NSData (track_item_t_Additions)
+ (NSData *)dataFromTrackItem:(track_item_t *)trackItem;
- (void)getTrackItem:(track_item_t **)trackItem;
@end
typedef struct track_item
{
// Some thing in here
}track_item_t;
track_item_t* head;
id songs = [[NSMutableArray alloc] initWithObjects:(track_item_t*) head];
This one is bad, changeTo initWithObject: or add a nil to the list, or
crashes will occur!
Anyway with the said category this could be something like:
NSMutableArray *songs = [[NSMutableArray alloc] initWithObject:[NSData
dataWithTrackItem:head]];
// some code to fill have an other list element.
after this try to fill this array using following code
[songs addObject:(track_item_t*) head];
And this would be:
[songs addObject:[NSData dataWithTrackItem:head];
But to add injury to insult; I think you should throw away your struct
completely, and make a FOOTrackItem class that inherits from NSObject
and go Objective-C all the way. Will save you allot of trouble. Unless
you already have a "thousands upon thousands of lines of code"-project
you want to reuse.
// Fredrik Olsson
Regards
Omer Riaz
--This message has been scanned for viruses and
dangerous content by Streaming Networks, and is
believed to be clean.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden