0

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.

user229044
  • 232,980
  • 40
  • 330
  • 338
Edgar
  • 898
  • 2
  • 12
  • 36

2 Answers2

1

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.

Alex Popov
  • 2,509
  • 1
  • 19
  • 30
  • `Car *myCar = [[Car alloc] init];` - creates a new Car object with the name myCar. But if it is: `Car myCar = [[DifferentCar alloc] init];` What will be the difference? I know that `[[XX alloc] init];` is for creating a new object, but what XX does there? – Edgar Feb 09 '15 at 11:48
  • @Edgar others may also be confused by this. Go ahead and ask this in a separate question. – Alex Popov Feb 10 '15 at 04:49
1

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.

tpatiern
  • 415
  • 4
  • 13