我继续得到这个预期的标识符或'('

时间:2014-09-10 14:45:13

标签: objective-c

我不断收到expected identifier or '('以下代码。我想为我的学校项目做一个例子。任何人的帮助将不胜感激。这是整个脚本。

  //
//  ViewController.m
//  Pocket Codez
//
//  Created by Dinesh1201 on 10/9/14.
//  Copyright (c) 2014 Dinesh and co. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
- (IBAction)Generate:(id)sender;
@property (strong, nonatomic) IBOutlet UILabel *Password;

@end

@implementation ViewController

- (IBAction)Generate:(id)sender {

struct label label = { .password = @"468392" };

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

错误位于

struct label label = { .password = @"468392" };

现在在编辑之后,并且说变量的类型不完整' struct label'

2 个答案:

答案 0 :(得分:1)

您似乎暗示它是问题的struct初始化,但是我不会期望该错误消息,但是初始化{的一个或多个成员的正确方法{1}}正在使用:

struct

当然,除非struct label label = { .password = @"468392" }; 是类的属性或实例变量(该语句没用,所以我觉得缺少必要的信息才能完全回答你的问题。)

答案 1 :(得分:0)

的问题:

  1. 您尚未定义结构并正在尝试使用它
  2. label.password = @"468392";
    • 您似乎正在尝试在NSString中设置struct个对象。编译器不允许你这样做。
    • 所以...在下面的解决方案中,我已将password声明为int

  3. 解决方案:

    //[1] define the struct
    typedef struct {
        int password;
        //...
    } labelStruct;
    
    @interface ViewController ()
    {
        //[2] make struct variable
        labelStruct label;
    }
    //...
    @end
    

    - (IBAction)Generate:(id)sender
    {
        //[3] apply value
        label.password = 468392;
    }
    

    检查:Objective-C Lesson 14: Structs and Unions了解详情。

相关问题