简体   繁体   English

xCode:尝试创建密钥窗口时,“未使用属性访问结果”

[英]xCode: “Property access result unused” when trying to make key window

(I'm using xCode 7.0.1 on El Capitan) (我在El Capitan上使用xCode 7.0.1)

So I have a main window and a button there and I also have another window which is not visible at first. 因此,我在其中有一个主窗口和一个按钮,并且还有另一个最初不可见的窗口。 The idea is to click the button and make the main window close and at the same time make the other window key and order it to front. 想法是单击按钮并关闭主窗口,同时使另一个窗口键并使其位于前面。 Here's my code so far: 到目前为止,这是我的代码:

- (IBAction)makeNewWindow:(id)sender {
    _newWindow.makeKeyWindow;
    _window.close;
}

However, when I enter this code, xCode gives me a warning saying "property access result unused - getters should not be used for side effects". 但是,当我输入此代码时,xCode会给我一个警告,指出“未使用属性访问结果-吸气剂不应用于副作用”。

I tried looking for the reason the error pops up but none of the questions were related to mine. 我尝试查找错误弹出的原因,但没有一个问题与我的问题有关。

NOTE: I am not linking the button to the two windows because if I do, it only keeps the task last given and does not complete the previous one. 注意:我没有将按钮链接到两个窗口,因为如果这样做,它只会使任务最后执行,而不会完成前一个任务。

Any thoughts on what I should do? 对我应该怎么做有什么想法?

Dot notation ( _newWindow.becomeKeyWindow ) is meant to be used to access properties, not to call methods, particularly methods that have side effects. 点表示法( _newWindow.becomeKeyWindow )用于访问属性,而不是调用方法,特别是具有副作用的方法。

Instead use method calling syntax: 而是使用方法调用语法:

[self.newWindow becomeKeyWindow];

It really is best, a best practice, to use self. 使用self.确实是最好的,也是一种最佳实践self. ( self.newWindow ) when using properties, not the variable directly with a leading underscore ( _newWindow ). self.newWindow )使用属性时,而不是直接_newWindow划线( _newWindow )处的变量。

After fiddling around for a few minutes I ended up with this piece of code that works. 摆弄了几分钟之后,我得到了这段有效的代码。 (Credit to @zaph for helping) (向@zaph提供帮助)

- (IBAction)makeNewWindow:(id)sender {
    [self.window close];
    [self.newWindow orderFront:(_newWindow)];
}

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

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