iphone - 使用 objective-c 传递和访问结构

标签 iphone c objective-c struct argument-passing

我想知道如何将结构传递给另一个函数并随后在被调用函数中访问该结构。我正在为 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
}

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

最佳答案

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

myUser->firstName

关于iphone - 使用 objective-c 传递和访问结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1064500/

相关文章:

循环浏览存储的名称并跳过空格

ios - 基于在第一个 View Controller 中按下的按钮,xcode 在第二个 View Controller 中打开 url

ios - 什么是 UILabel 默认 shadowRadius?

iphone - 当在应用商店状态提交应用时是无效二进制?

iphone - 我应该使用哪些搜索和比较选项来搜索汉字?

c - 简单的 C 双指针

c - 将密码字母替换为 QWERTY

ios swift - 键盘(掩盖文本字段等)和其他一些小部件的问题

iphone - 测试应用内购买可以在一台设备上运行,但不能在另一台设备上运行

ios - 在 iOS 7 中始终获得唯一的设备 ID