Objective-C tutorial?
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.
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.
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.
[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.
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"?
What's the difference between "NSString *foo", "NSString* foo", and "NSString * foo"?
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.
When compiled, the lines
NSString *foo;
NSString* foo;
NSString * foo;
All boil down to:
NSString*foo.
IMHO, NSString *foo; is easiest to read.
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: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!
I know some C, but I'm just not sure if Obj-C changes the way pointers are handled.
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.
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.
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.
How do I tell which methods are convenience methods, so I know when to explicitly retain objects?
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];
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];
Thanks. I'll study that document and see if I can fix my code.

