通和访问结构使用的Objective-C [英] Pass and access structures using objective-c

查看:110
本文介绍了通和访问结构使用的Objective-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道怎样的结构传递给另一个函数,并随后访问被调用函数的结构。我正在为iPhone开发,我使用结构的理由是,这样我可以最终通过数据结构到一台服务器正在建造的温度。

I want to know how to pass structures to another function and subsequently access that structure in the called function. I'm developing for the iPhone and the reason I'm using structs is so that I can eventually pass data as structs to a server being built in C.

下面的结构:

struct userInfo{
    NSString *firstName;
    NSString *lastName;
    NSString *username;
    NSString *email;
    NSString *ipAddress;
    double latitude;
    double longitude;
};

在这里,我只是取了一些CoreLocation数据和iPhone的IP地址一起某些用户inputed数据:

Here I'm simply fetching some user inputed data along with some CoreLocation data and the iPhone's IP Address:

- (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
}

在编译器将引发此错误:没有东西结构或联合会员要求'名字'。是结构超出范围时,我尝试访问它的第二个功能?

the complier throws this error: request for member 'firstName' in something not a structure or union. Is that struct out of scope when I try to access it in the second function?

推荐答案

您传递一个指针结构。使用 - >操作符,而不是点

You are passing in a pointer to a struct. Use the -> operator, not the dot.

myUser->firstName

这篇关于通和访问结构使用的Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆