0

I found this code and I was dumbfounded:

@interface MyNode : NSObject {
    Node *node;
}

@property(nonatomic,strong) Node *node;

@end

What is the behaviour of this code?
I think that property declaration of "node" ovveride an shadow previous declaration, isn't it?

2
  • Where is the "global variable" that you are mentioning? There's an instance variable and a property. Commented Mar 20, 2015 at 16:18
  • you're right, updated! Commented Mar 20, 2015 at 16:20

2 Answers 2

1

The behavior of your code is that you declared a property, and an instance variable. The @property(nonatomic,strong) Node *node; will have an instance variable _node auto synthesized by the compiler (this was introduced in Xcode 4.4).

Therefore, your class now has a node and _node instance variable.

Perhaps if you read the documentation then you won't be so dumbfounded when you come across programming language constructs!

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I've understand. But if I declare instance variable with name "_node", program compile anyway!! Why?
In that case, the compiler doesn't need to auto generate the "_node" instance variable, since you explicitly declared it. When the compiler auto generates the "@synthesize node=_node" line, it will use the instance variable that you explicitly declared.
@OpenUserX03 I am dumbfounded about the concise, clear documentation you mentioned.
1

I don't care what the behaviour of this code is. I care about coding standards and what they are good for. If your code depends on things that are so obscure that you have to ask questions here, then you are doing it wrong.

You should never, ever have an instance variable that doesn't start with an underscore character. Make it Node* _node instead. Now you don't have to worry about this anymore. The whole problem is gone.

Also, turn compiler warnings on so that the compiler can tell you when you are doing dangerous things.

1 Comment

What about the auto-synthesis of the property. Saying never is strict. And saying you don't have to worry about it doesn't explain why not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.