Help Me Remember Why


#1

Can some one please help me remember why we created valueInDollars as an instance variable and not a pointer to another object? I understand the difference between pointers and instance variables but I think I’m lost why one is chosen over the other. Why couldn’t valueInDollars be a pointer? Was this done just to show there is a difference between pointers and instance variables and how each is handled?

I went back through the book and couldn’t find the clarification I was looking for…

Thanks,
–Dave


#2

Welcome to the world of programming!

Programming is about representing, storing, receiving, transmitting, transforming, and displaying information.

Representing and storing information

In a programming language such as Objective-C, we represent information by using the fundamental data types that it provides. Bit is the most fundamental data type, upon which all other data types are built. (For example, char consists of 8 bits and short 16 bits.) It (bit) is a unit information; one bit can represent two distinct states, such as on or off, hot or cold, true or false, etc.

Objective-C offers several fundamental data types such as char, short, int, float, etc. Information such as time, mass, energy, and people’s salaries can be represented using these fundamental data types. But complex information (information that has structure) such as the velocity of an aircraft or the address of the headquarters of its manufacturer can not be represented by using one of the fundamental data types alone. For this, we combine the fundamental data types to form new, compound data types such as struct and class capable of representing such information.

Given a piece of information, if it can fit into one of the fundamental data types we just use the most suitable fundamental type to represent the information and we store the information in an object of that fundamental type. Otherwise, we need to use a compound data type and we store the information in an object of that compound type.

When we store a piece of information in an object of compound type, we (normally) use pointers to refer to that object; we don’t use pointers when we store the information in an object of fundamental type.

Objects of compound types; objects of fundamental types

Some objects of fundamental types:

char firstLetter = 'A';
unsigned long speed = 750;
const float PI = 22.0/7.0;
NSUInteger salary = 24000000;

Some objects of compound types:

NSString *radioStation = [NSString stringWithFormat:@"Triple J FM105.5 - ABC's youth station Down Under"];

Velocity *aircraftVelocity = [Velocity velocityWithSpeed:750 direction:directionVector];

const char *proverb = "more haste, less speed: you make better progress with a task if you don't try to do it too quickly.";

const long first7Primes [7] = {2, 3, 5, 7, 11, 13, 17};

If we use a struct as a compound type, we don’t always have to use pointers:

Some objects of compound types, not using pointers:

[code]struct LongInterval
{
long begin, end;
};

struct LongInterval interval = {2, 7};

struct Planet
{
char name [32];
char star [32];
unsigned equatorialDiameter; // KM
double distanceFromStar; // Million KM
unsigned int numberOfMoons;
};

struct Planet planet = {“Earth”, “Sun”, 12756, 149.6, 1};[/code]


#3

Thank you for the level of detail in your reply. I understand what you are saying, and I can see why you would want to set a pointer to itemName and serialNumber. What I’m getting hung-up on is that I expected dateCreated to be considered a fundamental data type.

I guess since NSDate is actually a float from 01-Jan-2001 you would want to treat it as a compound data type?

–Dave


#4

[quote] guess since NSDate is actually [color=#FF0000]a float[/color] from 01-Jan-2001 you would want to treat it as a compound data type?
[/quote]
NSDate is not a fundamental type; it’s actually a class type, which is a compound type.

See: NSDate Class Reference


#5

OK, I now understand the answer to my question. I apologize for asking what seems to be such basic question, but I had such a hard time wrapping my head around it all. With only prior experience in VBA, this is the third time I started digging in to Obj-C and iOS programming. Previously every time I thought I was getting the hang of it I realized I wasn’t and let my frustrations take over and walked away from it. I like to think I’m not the dumbest guy in the room, but learning Obj-C can make you a humble person. :wink:

–Dave