Edgar
Edgar

Reputation: 931

Newbie. Custom class

CustomClass *variableName = [[CustomClass alloc] init]; variableName.propertyName = @"Some text";

Could anyone explain this code step by step in human language?

Why if I want to send data to a property in CustomClass I am accessing it throught varibaleName.propertyName , but not through CustomClass.propertyName. I can not understand it.

If I want to send some data to a varibale in CustomClass wouldn't it be logically to show the path to that property = CustomClass.propertyName = @"Some text"; ?

*variableName - what is it for?

I am confused.

Upvotes: 0

Views: 41

Answers (2)

tpatiern
tpatiern

Reputation: 415

You access variableName.propertyName instead of CustomClass.propertyName because variableName is an instance of the class, while CustomClass is the class itself, not the object that you use.

For instance, you have 2 CustomClass objects, lets say variable1 and variable2. variable1.propertyName will be different from variable2.propertyName because they are different instances of the class, not the class itself.

Upvotes: 1

Alex Popov
Alex Popov

Reputation: 2519

There seems to be some confusion on the difference between an instance and a class. It's generally better to try and link complex ideas like this to real-world examples.

A Class could, for example, be Cars. Thus, you have a Car class. It will include information shared by all Cars. For example, instead of having propertyName it could have a "model" name. To access data about any given car you must first create it. That is what you do in the first line: CustomClass *variableName = [[CustomClass alloc] init];

In our example, we would write Car *myCar = [[Car alloc] init]; which creates a new Car object that we call myCar. Then, you can say myCar.model = "Civic". We do not want to make all cars be a "Civic", but specifically the myCar that we created. Do not be confused between a Class, which describes a general kind of object, and an Instance, which is the object itself.

Hopefully you now understand the last part of your question:

*variableName - what is it for?

This means that you have a reference to an instance of your CustomClass which is called variableName. In our example, this is myCar which you can then manipulate or change.

Upvotes: 1

Related Questions