简体   繁体   English

预期标识符

[英]Expected Identifier

Please can anyone tell me why this piece of code isn't working? 请谁能告诉我为什么这段代码不起作用? I've got a dictionary which contains UIViews with tables inside associated with the keys which are the names of the corresponding buttons (there are a lot of them). 我有一个包含UIViews的字典,其中的UIViews内部带有与键相关联的表,这些键是相应按钮的名称(其中有很多)。 So what I actually want to do is to change the view visibility on the corresponding button click. 因此,我实际要做的是更改相应按钮单击时的视图可见性。 But the issue is that the expression to do that is not accepted by Xcode and I get the Expected Identifier error. 但是问题是Xcode不接受执行该操作的表达式,并且出现了Expected Identifier错误。

- (IBAction)choosingButtonClicked:(id)sender {
    if ([sender currentTitle]) {
        [(UIView *)[self.selectionTables objectForKey:[sender currentTitle]]].hidden = ![(UIView *)[self.selectionTables objectForKey:[sender currentTitle]]].isHidden;
    }
}

First of all, with all due respect, I agree with trojanfoe comments. 首先,在所有适当的尊重下,我同意trojanfoe的评论。 Its not working because its not properly written. 由于无法正确编写,因此无法正常工作。

Now, lets try to streamline it with below code: 现在,让我们尝试使用以下代码简化它:

- (IBAction)choosingButtonClicked:(id)sender {
    NSString *title = [sender currentTitle];

    if (title) {
        UIView *selectionView = (UIView *)self.selectionTables[title];
        selectionView.hidden = !selectionView.isHidden;
    }
}

Your code is too complex, because of that even the author can't understand it. 您的代码太复杂了,因为即使是作者也无法理解。 If we re-write your code using local variables, it will look like: 如果我们使用局部变量重新编写您的代码,则它将类似于:

- (IBAction)choosingButtonClicked:(id)sender
{
    NSString *title = [sender currentTitle];
    if (title)
    {
        UIView *tempView  = (UIView *)[self.selectionTables objectForKey:title];
        [tempView].hidden = ![tempView].isHidden;
    }
}

If you check the code now, you can see that the following code is causing the issues: 如果立即检查代码,则可以看到以下代码导致了问题:

[tempView].hidden = ![tempView].isHidden;

Change your method like: 更改您的方法,例如:

- (IBAction)choosingButtonClicked:(id)sender
{
    NSString *title = [sender currentTitle];
    if (title)
    {
        UIView *tempView = (UIView *)[self.selectionTables objectForKey:title];
        tempView.hidden  = !(tempView.isHidden);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM