开发者

Objective-C / iPhone SDK Pointers Help

I'm new to iPhone development, and am sort-of getting there with pointers in Objective-C. Can anyone briefly explain pointers to me? I know that some native C types don't need pointers (i.e. are weakly typed), such as int.

Also, what's the difference between:

NSDate* theDate = [NSDate date];

and:

开发者_如何学PythonNSDate *theDate = [NSDate date];

?

Thanks again StackOverflow!


There's no difference between those two things. The asterisk denotes a pointer but may have whatever whitespace you like either side of it. E.g. the following is also valid:

NSDate * theDate = [NSDate date];

The creators of Objective-C chose to add objects to C by keeping them all on the heap (ie, they're things you explicitly create and destroy — there's no such thing as a local object in Objective-C) and referring to them via pointers. Pointers are just memory addresses. So 'NSDate *' isn't an NSDate, it's just a record of where in memory an NSDate object lives. Exactly like the difference between a street address and a house.

Primitive operations (like addition, multiplication, etc) work only on C primitive types, like int, short, float, etc. Technically the pointers you keep to Objective-C objects are primitive types, because the thing you actually have is the address not the object. But they're not generally very useful.

You operate on objects only by sending messages to them. The square bracket syntax means 'send this message to this object'. Which, when you're just starting, is sufficiently like a function call to just think of it as that. There's a distinction in that some things C does at compile time, Objective-C does at runtime. But you can just trust that they're being done for now.

Objective-C is relatively typeless — you declare pointers as being of a particular type but the operations you perform on them (ie, sending messages) act in exactly the same way irrespective of the type. That's why the 'id' type (which means any object) can exist. Objective-C is weakly typed in the sense that a variable of type id can have a pointer to any object assigned to it.

In practice, all objects descend from NSObject so it's often more useful to use NSObject * and an explicit cast when type hopping. That means you can use the things NSObject adds without compiler warnings, including Objective-C's reference counted memory stuff, the things for finding out whether an object can perform a particular message and so on.


none, but you might prefer the second one as it is much more explicit that theDate is the pointer

NSDate * theDate = [NSDate date];

does the same as well


The is no difference between NSDate* theDate; and NSDate *theDate;, but there has been wars waged over what is the preferred style, just as there has been war over if the { should be on the same line or the next. You could type NSDate*thedate; in an effort to please both sides :).

A pointer is a value with information about where to find another value.

  • char is an actual 8 bit integer value.
  • char* is a 32 or 64 bit value with information about where a 8 bit integer char can be found.

You can make a "pointer to" any kind of value by just suffixing any type with a *. You can even be recursive if you like; char** is a pointer to a pointer to a 8 bit integer value.

C has a concept of structs, that is that to group values, potentially of different types, that should be treaded as composite value. For example:

typedef struct {
    float CGFloat;
    float CGFloat;
} CGPoint;

Structs are treated as atomic values, this have some implications as this example shows:

CGPoint p1 = CGPointMake(1, 42);
CGPoint p2 = p1;              // a COPY of p1 is made.
STAssert(p1.x == p1.x, @"");  // Is true
p2.x = 10;                    // p1.x is NOT affected.
STAssert(p1.x == p2.x, @"");  // Is no longer true

As it turns out under the hood objects in Objective-C are actually structs. The actual members of these structs are abstracted away from you, but they still are.

You do want to be able to pass around objects in away so that changes made to in from one part of your code carries through to other parts of your code. This can be done if you only pass around pointers to the objects, not the actual objects themselves. Then each change through these pointers will all be done on the same actual object.

Hence: NSDate* not NSDate.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜