What do you think of when you hear the word object? Kind of a vague, general word, right? The idea is that objects can be anything. This is what we want for our programming purposes. We make and use objects that can be anything. We use these objects to fulfill whatever needs or purposes we have. To better understand the importance of objects in programming, lets dive in a little deeper.
Look around you right now and grab the nearest object. Hold it in your hand and look at it. What are some ways that you could describe the object to someone that is blind, and never seen it before? In my case, the object I'm using is my blanket on my bed. I might start describing this blanket as being red, and big, and having golden swirly designs on it. The blanket is pretty thick, and does a great job of keeping me warm.
I just did an adequate job of describing what the blanket is. Now if you think harder about our description of this object, what else could we say about it? The computer only knows what you tell it to know, so it is very important we understand the process of making objects. We just talked about what the object is, but what can be said about what the object has? The blanket has a texture, it has a color, it has a width, a height, and a thickness. The blanket also has a name, a manufacturer, a thread count, and even a tag on it somewhere describing the washing instructions.
Can you tell the difference between what an object is, and what an object has? This distinction is extremely important to understand very clearly. In object-oriented programming, objects are comprised of three important components:
1) The type
2) The property
3) The values
This makes sense. If we were comparing two objects that both had the same width, height, color, and shape, does it necessarily mean that they are the same type of object? Not necessarily. One object could be of type pan, and the other could be of type paper. The type is used to generally classify the object into groups.
The properties of an object describe what an object has. That shouldn't be too hard to grasp. Examples of properties are the shape, color, weight, size, etc. of an object. If it helps, think back to science class when you studied the properties of different minerals and rocks and things.
The final component of an object are the values. Properties and values are very closely related. The value is kind of the answer to the question that is raised by the existence of the property. The sheer existence of the height property doesn't necessarily mean that you know what the height ob that object is, right? It merely means that the object has a height. The value of the height property is where you understand what the object's height actually is.
So to quickly recap, object are comprised of three things. Without looking above, can you name them? Ok, an object is made of a type, properties, and values.
You may be wondering what in the world this object stuff has to do with actual programming, and the answer to that is that it has everything to do with programming. Let's think back to the very beginning, when I introduced you to variables. What are some basic types of variables? Integers, doubles, and booleans. Those three primitive (basic) data types are not objects. They don't have properties, they only have values. But if you'll remember, we also talked about another type of variable, the NSString.
Do you remember that? The NSString data type is an object because it has all three ingredients for what makes up an object. A type, properties, and values. That might sound confusing, but there is a lot more information about the NSString object that is available to us besides it's text value. We can ask the NSString object how long it is, or how much memory it uses, or what it's memory address is. Objects become really powerful (especially ones you make yourself) because you can put in and pull out as much information as you could possibly want or need.
So now that we have a better idea of how objects work, let's do a quick intro to what types of objects there are in iOS. Can you think of any besides NSString? Try and think about things that you see on your iPhone/iPad when you are using it. Anything that you see an object? Answer: everything is an object! The buttons that you press are objects. They have sizes, shapes, colors, positions, and things that they are supposed to do when pressed. Labels are objects, they have properties that you can set and adjust and change to fit your needs. Textviews, tableViews, scrollViews, webViews, mapViews, sliders, steppers, navigation bars, toolbars, activity indicators, alertViews, and even tabBars are all objects! Basically everything we do in iOS development (and many other computer programming languages for that matter) is object-oriented.
We could go on and on and on about how to make custom objects, and how to use them, and where to put them, and so on. I don't want to overwhelm you at this point, but I want to make it very clear that objects are very powerful, and that from here on out that will be a word you see more often than 'the'.
As we progress through different phases of development, we will get to understand objects a lot better. But for now, we are going to take advantage of all the amazing work that Apple has done and use the objects that they have made available for our use.
No comments:
Post a Comment