简体   繁体   中英

Clearing an object properly “getters should not be used for side effects”

I am following the well known Stanford online course in Objective C.

The course builds on a RPN calculator built using Model:View:Controller I have to create a clear button which I have using the following code (which works) into the View controller

self.display.text=@"0";

self.display.history=@"";

self.brain.clear;

in the model (referred to by the object "brain") the only instance variable is a NSMutableArray with the various things entered by the calculator user in it.

I have put a method called "clear" into the model that clears the array using the removeAllObjects method.

I am getting a warning from Xcode that "property access unused getters should not be used for side effects"

I have tried just deleting the brain object using lines like

self.brain dealloc; to no avail.

How should I clear down the brain object correctly?

Try using

[self.brain clear]; // <-- Calls method 'clear' of object brain

instead of

self.brain.clear; // <-- Access property 'clear' of object brain

When you need to call a method you should use the bracket "[" , "]" notation.

[object method];

eg: [human setAge:12] ;

When you need to access a property of the object you should use the dot notation.

object.propery;

eg: age = human.age;

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