我想知道如何將結構傳遞給另一個函數,然後訪問被調用函數中的結構。我在爲iPhone開發,我使用結構的原因是,我可以最終通過數據結構的服務器建在C.傳遞和訪問結構使用objective-c
這裏的結構:
struct userInfo{
NSString *firstName;
NSString *lastName;
NSString *username;
NSString *email;
NSString *ipAddress;
double latitude;
double longitude;
};
這裏我「M簡單地獲取一些CoreLocation數據和iPhone的IP地址以及一些用戶inputed數據:
- (IBAction)joinButton {
struct userInfo localUser;
localUser.firstName = firstName.text;
localUser.lastName = lastName.text;
localUser.username = username.text;
localUser.email = emailAddress.text;
localUser.ipAddress = localIPAddress.text;
localUser.latitude = currentLocation.coordinate.latitude;
localUser.longitude = currentLocation.coordinate.longitude;
[myNetworkConnection registerWithServer:&localUser];
}
功能處理的結構:
- (void)registerWithServer:(struct userInfo*)myUser {
printf("First name is: %s", myUser.firstName);//error when compiling
}
編譯器會拋出此錯誤:request for member 'firstName' in something not a structure or union
。當我嘗試在第二個函數中訪問它時,那個結構是否超出範圍?
沒有理由將Objective-C對象放入結構中。不要這樣做。或者讓一個對象保存你的數據,把它放在一個NSDictionary中,或者把所有的東西都轉換成本地C類型,比如你的結構的char *。 – 2009-06-30 17:01:39