简体   繁体   中英

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

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.

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 .

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

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.

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).

If you make it a property it is public. If you do not want the variable to be public, than do not use a property. Why do you not want to be public? You can have readonly property and make the writable in you .m via a private category.

You can put it in your .m file if you use iOS 5

@implementation MyClass {
     BOOL _myBool;
}

I think this is new to iOS5. But I have seen code like this too :

@implementation MyClass {
@private
     BOOL _myBool;
}

Hope this helps.

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