2011-09-19 42 views
0

我ManageMarketPacket.h有一个结构,就像如下:iphone,如何调用结构成员

#import <Foundation/Foundation.h> 
typedef struct ORIGINAL_QUOTA_DATA_tag{ 
    unsigned short id; 
    unsigned char exch;   
}ORIGINAL_QUOTA_DATA; 
@end 

和ManageMarketPacket.m有一个功能打算拿到ID:

- (unsigned short)getId:(NetWorkConnect*)netWokrConnect{ 
    //I want to get the id which have assigend in netWokrConnect.m 
    //I tried "return (netWokrConnect->oQuota).id; "is incorrect 
} 

在我NetWorkConnect.h,我所定义的结构:

#import <Foundation/Foundation.h> 
#import "ManageMarketPacket.h" 
@interface NetWorkConnect : NSObject{  
    ORIGINAL_QUOTA_DATA oQuota;  
} 

在NetWorkConnect.m,我分配oQuota.and在另一个文件中,我调用函数getId;

回答

0

有几件事情你需要改变才能使它工作。所有的

  1. 首先,你需要,因为你正在使用一个变量名idid是保留Objective-C的关键字来改变你的结构。我把它改成identifier,你会需要改变其使用变量

    typedef struct ORIGINAL_QUOTA_DATA_tag { 
        unsigned short identifier; 
        unsigned char exch;   
    } ORIGINAL_QUOTA_DATA; 
    
  2. 在你NetworkConnect类的任何方法,你需要添加一个@property和@synthesize你oQuota变量,以便您可以访问它从其他类像这样:

    NetworkConnect.h

    #import <Foundation/Foundation.h> 
    #import "ManageMarketPacket.h" 
    @interface NetWorkConnect : NSObject{  
        ORIGINAL_QUOTA_DATA oQuota;  
    } 
    
    @property (readwrite, assign) ORIGINAL_QUOTA_DATA oQuota; 
    

    NetworkConnect.m

    @implementation NetworkConnect 
    @synthesize oQuota; 
    
        // Rest of your Implementation here... 
    
    @end 
    
  3. 访问方法中的oQuota变量。你需要去改变它,像这样:

    - (unsigned short)getId:(NetWorkConnect*)netWokrConnect{ 
        // I want to get the identifer which have assigend in netWokrConnect.m 
        netWokrConnect->oQuota.identifer; 
    } 
    

    或者您可以使用.点语法,以及这是我比较喜欢像这样:

    - (unsigned short)getId:(NetWorkConnect*)netWokrConnect{ 
        // I want to get the identifer which have assigend in netWokrConnect.m 
        netWokrConnect.oQuota.identifer; 
    } 
    
+0

非常感谢你,但是有一点问题,“ - >”会得到警告“Instance variable'oQuota'is protected”,但点是正确的,再次感谢你.... – Gaojian922188

0

首先,你必须做一个财产在oQuota之外,使其在定义它的类之外可见。然后你可以简单地这样称呼它:netWorkConnect.oQuota.id