Gaojian922188
Gaojian922188

Reputation: 503

iphone - how to call struct in different file

I have defined a struct in a file,and now i want to use this struct in another file.i know it can use"::"to call the struct for C++, but for objective-c how to call the struct.

My.h file "ManageMarketPacket.h"

 #import <Foundation/Foundation.h>   
 typedef struct ORIGINAL_QUOTA_DATA_tag{
    short   id;
    char    exch;                  
}ORIGINAL_QUOTA_DATA;
@interface ManageMarketPacket : NSObject {        
}

My file "ManageMarketPacket.m"

#import "ManageMarketPacket.h"
@implementation ManageMarketPacket
@end

So my .m file has nothing,so my another file will use the struct,in my other file"NetWorkConnect.m"

#import "ManageMarketPacket.h"
@implementation NetWorkConnect
- (id)init{
    if (self==[super init]) {
       ORIGINAL_QUOTA_DATA quota;
    }
    return self;
}
@end

So my problem is ORIGINAL_QUOTA_DATA quota; it's incorrect...so I how to call the struct in NetWorkConnect.m?

Upvotes: 2

Views: 355

Answers (2)

Bourne
Bourne

Reputation: 10312

Your struct instance is local to the init method block and that is why you are not able to access it elsewhere. Declare it in the block you wish to access it and play around with it. If you want to access it throughout the class, declare it in the interface for the second .m file.

Upvotes: 0

gamozzii
gamozzii

Reputation: 3921

Declare quota in your header file for NetWorkConnect rather than in the init method. Then it will be an instance variable accessible from any instance methods just like any other instance variable.

e.g. if you put:

#import "ManageMarketPacket.h"
// ...

@interface ManageMarketPacket : NSObject {        
    ORIGINAL_QUOTA_DATA quota;
    // ...
}

in NetWorkConnect.h

then you will be able to reference it in other methods in NetWorkConnect.m (including init).

- (id)init{
    if (self==[super init]) {
       quota.id = 1;
       quota.exch = 'A'
    }
    return self;
}

-(void) updateMyQuote:(int)quotaId exch:(char) quotaExch {
      quota.id = quotaId;
      quota.exch = quotaExch;
}

-(void) doSomethingElseWithQuota {
      if (quota.id != someOtherId) {
           quota.exch = 'F';
      }
}

Upvotes: 1

Related Questions