I’m learning Objective-C and I’m confused on what the difference is between an instance of a class and an object –- are they the same ?
Heres an example:
NSString *name = [[NSString alloc]initWithString:@"Harry"];
*name is a pointer to the NSString class. @"Harry" is the value of the string. So is name an Object from the NSString class or is name called an instance of a class?
Another example from a Class I made:
Rectangle *rect = [[Rectangle alloc]init];
So *rect is a pointer. Is rect an Object or is it an instance of a class ?
In ObjectiveC, an instance of a class is always an object. And an object is always an instance of a class. An “object” is an instance of a class, a class which somewhere down the chain eventually inherits from
NSObject. When you declare a class with no superclass,NSObjectis the implicit superclass.nameis a pointer to an object, which more specifically is an instance ofNSStringthat has a value of “Harry”. All NSString’s are objects.Same here.
rectis a pointer to an object, which is an instance ofRectangle.