Objective-C tutorial?

Moderator
Posts: 1,560
Joined: 2003.10
Post: #46
AnotherJake Wrote:When you do:

op = [op initWithString: @""];

Remember that op isn't retained because it is being created with a convenience method which autoreleases it... i.e. it isn't being created with op = [[NSString alloc] init], which is automatically retained. (anything [[alloc] init] is automatically retained, otherwise it is usually considered to have been autoreleased upon creation)

AnotherJake Wrote:For instance, I am notoriously bad about returning retained objects in my custom inits rather than autoreleased objects, as I should.

I think there might be a misconception here. Cocoa's object ownership policy is spelled out here: http://developer.apple.com/documentation...3-BEHDEDDB

Memory Management Programming Guide for Cocoa Wrote:You own any object you create.
You "create" an object using a method whose name begins with “alloc” or “new” or contains “copy” (for example, alloc, newObject, or mutableCopy).

If you own an object, you are responsible for relinquishing ownership when you have finished with it.
One way to relinquish ownership of an object is to send it a release message. In Cocoa terminology, relinquishing ownership of an object is typically referred to as "releasing" an object.

If you do not own an object, you must not release it.

So, the "init*" methods don't directly have anything to do with memory management. It's just how you allocate/retrieve the object.
Quote this message in a reply
Moderator
Posts: 3,574
Joined: 2003.06
Post: #47
Hmm... I don't see where the misconception is, but I will look carefully over the rules again and see where I am off kilter. If you receive a new object which has been autoreleased by the creator then you'd better retain it or it will be gone at the next autopool release.
Quote this message in a reply
Moderator
Posts: 3,574
Joined: 2003.06
Post: #48
Oh, I think I see what you're getting at. Right, sorry, init has nothing to do with -- it's just alloc [or new or copy]. For some reason, when I'm thinking about it, I think in terms of the pattern: alloc (init) all the time -- goofy me.

[edit] Also, it was late last night, and the initWithString example is not what I meant to say. I meant to say op = [op stringByAppendingString: @"blah"];, which is actually what his code is doing. stringByAppendingString is creating a new autoreleased string which must be retained or it will be gone later.

[edit 2] Also, in his code, doing op = [[op stringByAppendingString:@"blah"] retain]; will orphan the original op. At the very least, it should be op = [[[op autorelease] stringByAppendingString:@"blah"] retain];

[edit 3] Sheesh! Yeah, another edit... Also, I didn't mean to say my custom inits, I meant to say my custom convenience creators.
Quote this message in a reply
Member
Posts: 28
Joined: 2008.06
Post: #49
I appreciate the offer, Willem, but I don't really feel comfortable giving out my physical location. Thanks anyway!

What's the difference between "NSString *foo", "NSString* foo", and "NSString * foo"?
Quote this message in a reply
Moderator
Posts: 623
Joined: 2007.09
Post: #50
There isn't one.

When compiled, the lines

NSString *foo;
NSString* foo;
NSString * foo;

All boil down to:

NSString*foo.

IMHO, NSString *foo; is easiest to read.

- Lincoln Green
http://www.binkworks.com/
Quote this message in a reply
Member
Posts: 28
Joined: 2008.06
Post: #51
Thanks. I like it that way, and the tutorial I'm using says "NSString* foo" and I was just wondering if it was safe to change.
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2007.02
Post: #52
Quote:What's the difference between "NSString *foo", "NSString* foo", and "NSString * foo"?
Questions at this level would indicate that you would benefit from some basic tutorials in C itself, removing Objective-C and Cocoa entirely from the picture. Maybe get a beginners C book first? There are tons of C tutorials on the web as well!
Quote this message in a reply
Member
Posts: 28
Joined: 2008.06
Post: #53
I know some C, but I'm just not sure if Obj-C changes the way pointers are handled.
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2007.02
Post: #54
Think of Objective-C as a super set of C. It does everything C does with a bunch of stuff added on top. Sort of the same as C++ or C#. Same base layer with goodies added.
Quote this message in a reply
Member
Posts: 28
Joined: 2008.06
Post: #55
Ok. I read somewhere that it was a subset of C++, so I wanted to make sure I didn't break anything. Thanks for clearing that up.
Quote this message in a reply
Moderator
Posts: 3,574
Joined: 2003.06
Post: #56
computergeek6 Wrote:I read somewhere that it was a subset of C++,
Absolutely not! Obj-C has nothing to do with C++ as a language. It is *compatible* with C++.

BTW, not that it matters, but my personal preference for pointers is:

MyPointer *pointer;

As already mentioned, pointers are the same in Obj-C as they are in C, but I'd like to add that not only are pointers used the same way, the entire language relies on them at its very core. id is a pointer, which means all objects are represented as pointers. It is a good idea to remember that fact, because it can be helpful when you drop into C and want to call back to an Obj-C instance.

NSString *string;

string is a pointer.
Quote this message in a reply
Member
Posts: 28
Joined: 2008.06
Post: #57
How do I tell which methods are convenience methods, so I know when to explicitly retain objects?
Quote this message in a reply
Moderator
Posts: 3,574
Joined: 2003.06
Post: #58
As has been said, you need to study up on it. The link ThemsAllTook put up is the authority: http://developer.apple.com/documentation...3-BEHDEDDB

Quick rule of thumb: Anything you create yourself with an alloc (or new or copy) is retained. Everything else should be assumed to be autoreleased, and therefore you need to retain it if you are going to keep it for anything more than immediate temporary use (like within the current method).

This does not count @"some string" because that is an NSString string constant and is in memory and cannot be removed, like any other constant.

In the code you posted, there is a lot of:

op = [op stringByAppendingString: @"blah"];

Which should be:

op = [[[op autorelease] stringByAppendingString:@"blah"] retain];
Quote this message in a reply
Member
Posts: 28
Joined: 2008.06
Post: #59
Thanks. I'll study that document and see if I can fix my code.
Quote this message in a reply
Post Reply