Re: Inconsistent Memory Management Rules
Re: Inconsistent Memory Management Rules
- Subject: Re: Inconsistent Memory Management Rules
- From: Denis Stanton <email@hidden>
- Date: Wed, 16 Apr 2003 11:24:25 +1200
On Wednesday, April 16, 2003, at 08:32 AM, alex wrote:
>
Hi Denis,
>
>
string must have been declared somewhere else such as in the header.
>
As you can see it's nothing in the code you quoted, it's not even
>
dynamically typed as id and certainly without being declared somewhere
>
else wouldn't work at all the way quoted.
OK, so there has to be a previous declaration. What does it look like?
In my limited knowledge I can only think it would consist of the line
NSString * string = [[NSString alloc] init]
which is the statement that Lloyd criticized in the first place. As he
pointed out, this statement created an immutable empty string which is
not much use to anyone
This is my confusion. You can't refer to an immutable string until you
have created it, but it creating it makes an immutable string so you
can't do anything with it.
Is the point here that
NSString * string = [[NSString alloc] init]
is a waste of time and you should either be working with mutable
strings as in
NSMutableString *string = [[NSMutableString alloc] init];
[string appendString: @"Steve Jobs"];
or you should use an init method that makes a useful string in the
first place
NSString *string = [[NSString alloc] initWithString: @"Yosemite"];
At risk of holding myself up to ridicule, here is a section from a
program I wrote. I recognized the example that started this thread as
resembling my code. I was uneasy about this code, bit it "works" - to
the extent that my program hasn't crashed yet. The code is simply
taking the elements from an array of strings and concatenating them
with a single space in between each item. I am aware that there is
probably a single method that would do this, but I haven't found it. In
java there is .componentsJoinedByString. For the sake of education
could you care to comment on the code below? My understanding from
this thread is that the code below leaks at almost every step.
// take date from words array
NSString *myDateString = [words objectAtIndex: 0];
int i;
for (i = 1; i < [words count]; i++) {
myDateString = [myDateString stringByAppendingString: @" "];
myDateString = [myDateString stringByAppendingString: [words
objectAtIndex: i]];
}
Thank you for your time
Denis
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.