Beginner Objective C syntax observation [duplicate] - objective-c

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.

Related

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

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.

Why do #synthesize variable names begin with an _? [duplicate]

This question already has answers here:
What does #synthesize window=_window do?
(3 answers)
Closed 9 years ago.
I'm just starting to use Objective-C and I need to clarify something
When I #synthesize a #property, it is common convention to do the following:
#interface Class : ParentClass
#property propertyName
#end
#implementation
#synthesize propertyName = _propertyName;
#end
I've seen plenty of questions and answers suggesting that "_propertyName" is widely accepted as the "correct" way to synthesize properties. However, does it serve ANY purpose? Or is it merely to increase readability and identify instance variables?
It makes it so that if you accidentally leave off "self." you get a nice compiler error instead of silently not having your methods called.
From http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html
You Can Customize Synthesized Instance Variable Names
As mentioned earlier, the default behavior for a writeable property is
to use an instance variable called _propertyName.
If you wish to use a different name for the instance variable, you
need to direct the compiler to synthesize the variable using the
following syntax in your implementation:
#implementation YourClass #synthesize propertyName =
instanceVariableName; ... #end
Also:
Note: The compiler will automatically synthesize an instance variable
in all situations where it’s also synthesizing at least one accessor
method. If you implement both a getter and a setter for a readwrite
property, or a getter for a readonly property, the compiler will
assume that you are taking control over the property implementation
and won’t synthesize an instance variable automatically. If you still
need an instance variable, you’ll need to request that one be
synthesized: #synthesize property = _property;
By doing this the generated accessor actually got to know which variable(iVar) to use.
Yea, It increases the readability & also separates the private & public variables to understand & use. Private variable of Class generally written in "propertyName" format.You can say it is a coding convention where Private Variable Names use '' as prefix and Public Variables or Property Names are lowerCamelCase.

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

Whats the use of having synthesize statement [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Obj-C #synthesize
Synthesized property and variable with underscore prefix: what does this mean?
Whats the use of having synthesize statement like this
#synthesize popOverTableData = _popOverTableData;
Thanks!
It simply states that the backing variable for popOverTableData is _popOverTableData. Normally without this, and just doing #synthesize popOverTableData means that the backing variable is called the same.