How can I pass a struct to NSOperation Init
I want to know how I can pass a pointer to a struct to a custom init method of nsoperation. It seems to expect to be passed an item of开发者_开发知识库 type ID.
Is this possible to do?
typedef struct mystruct
{
int a;
int b;
}mystruct;
mystruct myitem;
MyNSOperation *op=[[MyNSOperation alloc]initwithdata:myitem]; //can't do this, not of type id
-(id)initwithdata:(mystruct *)thestruct
{
}
If you want to pass a struct as an object, you can use NSData
NSData *data = [NSData dataWithBytes:&myitem length:sizeof(myitem)];
and get it back to a struct with
[data getBytes:&myitem length:sizeof(myitem)];
This is not architecture independent.
Your init method should look like this:
-(id)initwithdata:(mystruct)thestruct
{
}
Or you should pass reference to your structure:
MyNSOperation *op=[[MyNSOperation alloc]initwithdata:&myitem];
Choose wisely.
精彩评论