使用 Objective-C 的传递和访问结构

发布于 2024-07-26 00:14:43 字数 1122 浏览 4 评论 0原文

我想知道如何将结构传递给另一个函数,然后在被调用函数中访问该结构。 我正在为 iPhone 进行开发,我使用结构的原因是这样我最终可以将数据作为结构传递到用 C 构建的服务器。

结构如下:

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

这里我只是获取一些用户输入的数据以及一些CoreLocation 数据和 iPhone 的 IP 地址:

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

编译器抛出此错误:请求非结构或联合中的成员“firstName”。 当我尝试在第二个函数中访问该结构时,该结构是否超出范围?

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.

Here's the structure:

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

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];
}

function handling the struct:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏天碎花小短裙 2024-08-02 00:14:43

您正在传递一个指向结构的指针。 使用-> 运算符,而不是点。

myUser->firstName

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

myUser->firstName
满地尘埃落定 2024-08-02 00:14:43

我忍不住认为你应该真正将其制作为具有属性的适当 Objective-C 对象 - 更多工作,但随后它会表现得更好并生活在 NSObject 生态系统中。

I can't help but think you should really make that a proper objective-C object with properties - more work but then it'll all behave better and live within the NSObject ecosystem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文