MGM
MGM

Reputation: 2375

Objective-C coding style (rather semantics)

I am new to Cocoa and I want to make sure that the style and code I'm using is of the proper form for my purposes.

Specifically in the header (at mark a)) what are the effects of setting the variable outside of @interface?

And secondly what are the effects of using a variable from an instance (at point b)) without registering it inside the class declaration?

Header file:

    #import <UIKit/UIKit.h>

    ///////////// a) Is this good use?
    int myint; 
    /////////////        

    @interface InstancecheckViewController : UIViewController
    - (IBAction)plusone:(id)sender;
    @property (weak, nonatomic) IBOutlet UILabel *counting;
    @end

Implementation:

    #import "InstancecheckViewController.h"
    @interface InstancecheckViewController ()
    @end
    @implementation InstancecheckViewController
    @synthesize counting;

    ///////////////////// b) is this good use?
    - (void)resetit {
        myint = 0;   
    } 
    /////////////////////

    - (IBAction)plusone:(id)sender {
        myint ++;
        if (myint >10){
            [self resetit];
        }
        NSString* myNewString = [NSString stringWithFormat:@"%d", myint];
        counting.text = myNewString;
    }
    @end

Edit
Thanks everyone for your comments. I think I have now properly redefined the instance and the integer in .h

    @interface instancecheckViewController : UIViewController
    {
    @private
    int myint;
    }
    - (IBAction)plusone:(id)sender;
    - (void)resetIt;
    @end

Upvotes: 1

Views: 131

Answers (3)

OutOnAWeekend
OutOnAWeekend

Reputation: 1453

Just my 2 cents. It is a global variable and is not quite good. It is better not to get into the habit of using it. You will seldom see codes which use global variables. If you learn all the basics of Objective-C, i.e. protocols, categories, extensions you will see that you seldom need to have global variables anyway.

Upvotes: 0

justin
justin

Reputation: 104698

with:

///////////// a) Is this good use?
int myint; 
/////////////        

you've declared a global, mutable variable. it is not the same as an instance variable. this should not be used. furthermore, it's a definition -- this approach will result in linker errors.

with:

///////////////////// b) is this good use?
- (void)resetit {
    myint = 0;   
} 
/////////////////////

you're writing to a global, mutable variable. this is not thread safe. that's not to imply that an ivar is implicitly threadsafe, but it is generally more safe thatn global accesses because its access is restricted to the instance.

just declare it as an instance variable :)

Upvotes: 1

user529758
user529758

Reputation:

By this, you're declaring a global variable which is shared across instances. So if you change it from an instance of your class, it will affect all the other instances. I'm not sure whether you want this; I'd suggest you to use an instance variable instead, if you don't need explicitly the behavior described above.

Upvotes: 0

Related Questions