Setters and Getters


#1

Please clarify for me.

  • (void) setItemName:(NSString *)str
    {
    itemName = str;
    }
  • (NSString *) itemName
    {
    return itemName;
    }
  • (void) setSerialNumber:(NSString *)str
    {
    serialNumber = str;
    }
  • (NSString *) serialNumber
    {
    return serialNumber;
    }
  • (void) setValueInDollars:(int)i
    {
    valueInDollars = i;
    }
  • (int) valueInDollars
    {
    return valueInDollars;
    }
  • (NSDate *) dateCreated
    {
    return dateCreated;
    }

I understand (void) means to not expect a return type. Im understanding that setItemName and setSerialNumber are setter methods. What is (NSString *)str doing?
Am I correct in saying “Set itemName to *str” followed by “Get *itemName” and “Set serialNumber to *str” and “Get *serialNumber”? (NSString *)str is in both setters so thats confusing me; what is its function in this statement.

This paragraph from the book has tripped me up and Im confused:

Notice that the setter methods assign the appropriate instance variable to point at the incoming object, and the getter methods return a pointer to the object the instance variable points at. (For valueInDollars, the setter just assigns the passed-in value to the instance variable, and the getter just returns the instance variable’s value.)

So after typing this out and before posting it, I may finally wrapped my brain around this concept. Please confirm if I’ve understood correctly in personification form:

  • (void) setItemName: (NSString *)str
    {
    itemName = str;
    }

Im a setter and I want to set some info. Im not expecting anything in return when I do my job. At this moment, my job is to set itemName’s value. Ive been told to look at str and my job is to see the instance variable itemName and store a value in that memory address’s. My boss gave me a 9 so Im gonna take this 9 and shove it into the itemName instance variable and I can only see itemName because str pointed me to look there. (in effect, this statement from the “-” to the “}” this is acting like a simple function)

  • (NSString *) itemName
    {
    return itemName;
    }

Here Im a getter and I want some info. Im expecting a pointer when its all said and done. Im told to look at itemName see its location and report back with its memory address to the boss so the boss can know the value of itemName.

The same goes for serial Number.
Is this correct or did I fall off some cliff and hit my head trying to understand?