How to declare an instance variable in Objective C? [duplicate] - objective-c

This question already has answers here:
Declaring instance variables in iOS - Objective-C
(2 answers)
Closed 6 years ago.
I am looking to do something like this:
#interface Player : NSObject
#properties int hitPoints = 200,
mana = 100;
#end
So that every time I initiate an object of class Player, that object will have
those variables with those specific values.
Do I need to declare this in the #implementation section?
I know that i can set those variables in the init faction
but i wonder if there is another way.
I am new to Objective C, and OOP in general.

If you want your properties to be public you would want to declare them in your header file and then, as you said, in the init method of your implementation file you could give them their default values.

Related

What are the circumstances that cause a #property to not automatically create an instance variable? [duplicate]

This question already has answers here:
Under what conditions is #synthesize automatic in Objective-c?
(5 answers)
Closed 7 years ago.
I've only been in the Objective-C & Cocoa world for a year, so I wasn't around when properties weren't automatically synthesized
Whenever I create new classes in our project, I can declare #property BOOL testFlag without declaring an instance variable. Then, in the implementation, I could either call self.testFlag, [self testFlag], [self setTestFlag:NO]. But, I could also call _testFlag. I always assumed because properties are automatically synthesized with instance variables, that's why I can just append a "_" underscore before the property name to directly access the instance variable.
However, I have been refactoring really old files, that clearly were created before auto-synthesizing was a thing
So now in the header, I'd see something like this:
#interface testClass
{
BOOL _testFlag
}
#property BOOL testFlag;
In the implementation, there could be a custom setter/getter:
#implementation testClass
#synthesize testFlag = _testFlag;
-(void)setTestFlag:(BOOL)testFlag
{
_testFlag = testFlag;
}
-(BOOL)testFlag
{
return _testFlag;
}
But I thought because of auto-synthesizing, I could remove the _testFlag declaration in the header, and I could remove the #synthesize in the implementation. But when I do this, there are just a truck ton of errors; wherever somebody was directly trying to access the ivar _testFlag. (This actually includes the custom getter/setter above ^^, too)
Is there perhaps something I am missing with the project settings that prevent this old file from generating an implied instance variable for properties?
Remove the getter and setter to have automatic synthesis of the backing variable. When you supply both for the property, the assumption is that you're going to look after storage yourself.

What is the purpose of using an underscore in Objective-C iOS 6.1? [duplicate]

This question already has answers here:
How does an underscore in front of a variable in a cocoa objective-c class work?
(9 answers)
Closed 9 years ago.
I am new to iOS development. I am creating a simple app that calculates the payout of a horse bet based on the user's bet placed and the odds they enter for the horse. In XCode (4.6.3), I have the following method which is used to calculate increase the user's bet by 1 when the button is clicked:
-(IBAction) addBet:(id) sender {
self.bet = [_betTextField.text intValue];
if(self.bet > 0) {
self.bet += 1;
self.betTextField.text = [NSString stringWithFormat:#"%d", self.bet];
}
}
I have declared the betTextField variable as follows in my header file: #property (weak, nonatomic) IBOutlet UITextField *betTextField;
XCode gives me an error if I do not use the underscore before the betTextField.text variable. Why is this?
The purpose of the underscore is to make it so that you can't accidentally use the ivar directly when you meant to use the property instead.
Because without the underscore the name doesn't really exist. This is because of what #property means and what the compiler does for you behind the scenes. Basically, your property can be accessed by using self.betTextField (which uses the automatically defined accessor methods) or _betTextField (which directly accesses the instance variable).

What's with the # sign in Objective C? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does the # symbol represent in objective-c?
I'm an ANSI C expert, taking my first steps to learn Objective C.
How should I think of/read the "#" that crops up everywhere?
At first, I thought of it as a compiler directive - "this in an instruction to the compiler to generate getter/setter" for "#synthesize someProperty".
But the "#" before strings doesn't seem to fit that model, and seems basically redundant.
Also would be glad of any advice for C experts on "how to quickly learn Objective C". Thanks,
Peter
# is just a character unless you use it with full context
#interface //For declaring a class
#property // For creating a property
#synthesize // For synthesising property
#implementation //For implementing a class
#end //For ending #interface or #implementation
#"Some String" //Represents a string
#try
#catch
and so on.
So simple # is nothing but when combined it becomes the directive

Objective c synthesized property ivar [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How does an underscore in front of a variable in a cocoa objective-c class work?
Prefixing property names with an underscore in Objective C
When we declare a property and then synthesize it like this for example:
#synthesize name = _name;
So, the _name is an instance variable for the name property we are gonna use in the following implementation.
My question is why we need this ivar and what would happen if i didn't create the _name ivar?
Thank you.
My understanding is that there is a default ivar name, which is the same as the property name. One of the reasons for specifying your own, with an underscore, is to eliminate ambiguity in your setter:
-(void) setFoo:(id) foo {
// here, foo should ONLY refer to the passed-in var
// If your ivar is the same, it is ambiguous.
// If your ivar is _foo, then there is clarity.
}

Beginner Objective C syntax observation [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does #synthesize window=_window do?
How come when you #synthesize something like buttonPressed , You need to do this:
#synthesize buttonPressed = buttonPressed_;
I've been following some tutorials and this keeps coming up. Why?
You do not have to do it that way.
By default, #synthesize variableName does work, if your synthesized accessors shall have the same name as your instance variable.
In your example, the instance variable is called buttonPressed_ but your accessor methods will omit the _ and thus just be called setButtonPressed and buttonPressed.