简体   繁体   中英

Property access result unused

I just started learning Objective-C yesterday, and i can't quite figure out this warning:

Property access result unused - getters should not be used for side effects

Using this code (line 3 gives the warning, self.addItem()):

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField == self.itemTxt) {
        self.addItem;
        self.itemTxt.text = @"";
    }
    return YES;
}

- (IBAction)addItem {
    [self.model addItemToArray: _itemTxt.text];
    _itemTxt.text = @"";
}

Why am I getting this warning?

What is addItem. According to your code, it seems you're trying to call that function so use this:

[self addItem];

One more thing basically, if you see in depth about IBAction, you'll find it's void. Normally, we use IBAction when we want to use IB for event fire.

So, you can use(if you need to call manually)

- (void)addItem {
    [self.model addItemToArray: _itemTxt.text];
    _itemTxt.text = @"";
}

Hope, it'll enhance your knowledge and will assist in your warning.

To call the function use

[self addItem];

When you're using self.addItem you're accessing a variable named addItem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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