views:

870

answers:

4

I really need some clarification — I have a few questions and I'm all mixed up right now.

Here is a simple class interface:

#import <UIKit/UIKit.h>

@interface Car : NSObject{
    NSInteger carID;
    NSString *carName;
}

@property (nonatomic, assign) NSInteger carID;
@property (nonatomic, copy) NSString * carName;
@end
  1. Why is carID not declared as a pointer?
  2. Why does it use "assign" for carID instead of "copy"?
  3. Why even declare class members as pointers in the first place? (In my main program, my Car object will be used as a pointer.)
+4  A: 

NSInteger is simply a typedef for a primitive type (int on 32-bit, long on 64-bit) — it is not an object, and can as such not be retained or copied.

Class members are always pointers; you never pass the "real" objects around; as that would be, at best, unmanageable.

Edit: To expand on the last paragraph: Objective-C class instances always exist on the heap, never on the stack; this is to facilitate things like reference counting and self-managed object life cycle.

This also means that it's very hard to accidentally copy an object; but on the flip side it can be somewhat easier to accidentally dispose of an object you still need. Still, the latter is more readily debugged (as it causes a nice, big crash (at best, anyway)) than the last (which at worst causes a slow leak).

Williham Totland
Other plain old datatypes that look like classes: `NSUInteger` (unsigned long), `CGPoint`, `CGRect`, `NSRange` (structs), `CGFloat` (double)
Williham Totland
Ah ok Because in c++ you can have integer pointers.But not in Objective C.
qstar
Actually, you can have integer pointers in Objective-C, since you can in C. However, pointers to primitives are generally used when you want another function/method write a primitive value directly to a particular address. When you're using Objective-C properties, there's no need to do that, since you're copying whatever value is provided into the instance variable.
Quinn Taylor
A: 

The property for carID is not really correct. For types that are not pointers, the correct definition looks like:

@property (nonatomic) NSInteger carID;

It's always going to be copying a value anyway, but "copy" has a very different meaning in properties - for objects it's going to call [object copy] when that property is used to set a new value.

Or you could drop off the nonatomic, but then the property is more expensive to call (by some small amount). Just leave in the nonatomic unless you have a good reason not to.

Kendall Helmstetter Gelner
A: 

Thanks guys!

So in Objective-C , you have int and Pointer Int.

How do you declare these in objective C

-int being a regular int.

-Pointer Int being an object representation of an integer. Since it is an object, it can also point to pointers*. Right?

And Pointer Int pointers can point to pointers of any type If I wanted to. Right? It will cause a crash if it doesn't point to a Pointer int. But it will compile successfully, Right?

But in what scenarios would I prefer using a regular int to a Pointer Int?

qstar
"Pointer Int" in this case would probably be an NSNumber object. It would be declared as NSNumber *myInt. Of course, to actually get the integer value out of it, you'd have to call [myInt intValue]. If you're just wanting a counter, you'll just want a primitive int. The only reason for an NSNumber is to pass it to something that requires objects, such as an NSArray or NSDictionary.
BJ Homer
A: 

I would like to add some clarification why you would want to use:

@property (nonatomic, copy) NSString * carName;

instead of

@property (nonatomic, retain) NSString * carName;

The copy keyword implies language semantics that you want to have a COPY of the NSString passed into your current object reference. So the pointer does not change (that is why you don't have to release the object ref).

The retain keyword makes it so that you get the pointer which will be retained because the pointer reference changes for this data member (and the current one will be released). Copying a NSString might not be a considerably heavy operation, so copying NSString is used often. You have to be careful what type of property you declare as copy. There might be a considerable amount of effort to produce a copy of types like Dictionaries etc (see shallow, deep copy etc).

Hope that helps!

fnCzar
"pointer reference changes for this data member"- Really, why would it change?- i would think that once a memory object is allocated in heap, it remains there no?
qstar
what I meant is that it now points to something else, not what it was pointing to before!
fnCzar
just trying to wrap my head around thisok thank you so much
qstar
I read that and I still don't get what the point of using "copy" is..
Mk12
Mk12, the reason you want to use copy sometimes is so that you can get the same information assigned to a variable without having to worry about the place where the information came from, getting released and losing the data. Copy will put a 'copy' of the date in memory that you control.
fnCzar