How do I create a BOOL instance variable in objective c?

55,036

Solution 1

I need to create an instance variable of type BOOL with a default value of FALSE in one of my objective c classes. How do I do this?

Assuming you are using the current Xcode, you can declare the ivar in the implementation like this:

@implementation MyClass
{
@private
    BOOL myBool;
}

I've seen people define it in their .h file but I don't really need this variable to be public.

Historically, you had to declare instance variables in the @interface because of the way classes were implemented. This is now necessary only if you are targeting 32 bit OS X.

Additionally, should I make it a property?

That depends. You should definitely make it a property if it is part of your class's external API (for unrelated classes or subclasses to use. If it's only part of the object's internal state, you don't need to make it a property (and I don't).

Or should I ever not make something a property? Thanks.

If you are not using ARC and the type is an object type (BOOL is not) you should always make it a property to take advantage of the memory management of the synthesized accessors. If you are using ARC, the advice on the Apple developer list is to make stuff that is part of the API properties and stuff that is internal state as ivars.

Note that, even for internal state, you might want to use KVO, in which case, use a property.

If you declare this BOOL as a property and synthesize it, you do not need to explicitly declare the ivar. If you want to make the property "visible" only to the class itself, use a class extension

@interface MyClass()

@property (assign) BOOL myBool;

@end

@implementation MyClass

@synthesize myBool = myBool_;  // creates an ivar myBool_ to back the property myBool.

@end

Note that, although the compiler will produce warnings for the above, at run time, any class can send setMyBool: or myBool to objects of MyClass.

Solution 2

If you want to make a private variable you can use the power of categories. Make a class MyClass for example and in the .m file do the following:

#import "MyClass.h"

@interface MyClass() //This is an empty category on MyClass class

@property (nonatomic, assign) BOOL myBool;

@end


@implementation MyClass

@synthesize myBool = _myBool;

-(void)myMethod {
   self.myBool = YES; //this is using the property
   _myBool = NO; //this is the instance variable, as @synthesize creates an instance variable for you
   // both are private
}

@end

Solution 3

BOOL's are declared as iVars with a simple BOOL myBool, and as a property with @property (nonatomic, assign) BOOL myBool. Take note though, BOOLean values are inititalized to NO (nil). If you need a BOOL to be accessible from other classes, or "global" in the m file, use an iVar or a property, otherwise just declare them as scoped vars in an individual method.

Solution 4

You can put the declaration in the .m file.

Whether you make a property out of it, depends on what you want. As a property, you can later apply setter/getter that include checking/guards/whatever. In addition, you can apply key-value-access.

If you do not need one of this, there is no real need for an property (but a good tradition: you never know, how your code will evolve).

Share:
55,036
Nosrettap
Author by

Nosrettap

I recently graduated from Duke University as a computer science and economics double major. I am now working full time as a software developer. I am proficient in Objective-C and Java, and I know (to some degree) C, C++, Python, Perl, SML, Assembly, HTML, CSS, JavaScript.

Updated on July 09, 2022

Comments

  • Nosrettap
    Nosrettap almost 2 years

    I need to create an instance variable of type BOOL with a default value of False in one of my Objective-C classes. How do I do this? I've seen people define it in their .h file but I don't really need this variable to be public. Thus shouldn't I put it in my .m file? Additionally, should I make it a property? Or should I ever not make something a property? Thanks.

    • Parth Bhatt
      Parth Bhatt almost 12 years
      Try: -(void)viewDidLoad { BOOL flag = NO; }. Also once you want to declare in it .m file you cannot property synthesize it from a .m file.
    • Nosrettap
      Nosrettap almost 12 years
      Where should I define flag? Doesn't it make sense in the .m file? However, I can't figure out the syntax for it. Thx
    • iphonic
      iphonic almost 12 years
      define the property in .m class as you do in .h .. it will make the bool private to the class not accessible from outside.
    • Parth Bhatt
      Parth Bhatt almost 12 years
      flag is the BOOL instance variable in objective c which is .m file as you want. It is only for the method viewDidLoad.
    • Lomithrani
      Lomithrani almost 12 years
      @Parth, that's not an instance variable, that's a local variable, and instance variables declared in an implementation file can certainly have declared properties associated with them.
  • jrturton
    jrturton almost 12 years
    BOOL ivars initialized to YES? Do you want to rethink that statement?
  • jlehr
    jlehr almost 12 years
    "If you make it a property it is public." Actually, no. Properties can be declared privately in a class extension or category.
  • Pfitz
    Pfitz almost 12 years
    But if you do nothing they are public.
  • jlehr
    jlehr almost 12 years
    If you do nothing, you have nothing. What determines whether a method or a property is public is where it's declared. If it's declared in the header, it's publicly visible; otherwise it's not. (There's no runtime enforcement though.)