2011-09-19 51 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