discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSMutableString -initWithFormat appends to existing text


From: H. Nikolaus Schaller
Subject: Re: NSMutableString -initWithFormat appends to existing text
Date: Wed, 18 Apr 2018 09:52:41 +0200

> Am 18.04.2018 um 09:42 schrieb Mick Bert <micbert75@gmail.com>:
> 
> Hello.
> Can I make question about how to use API, in this list?
> I decided to use GNUstep for all the working activity where I have the
> chance, just to practice my obj-c/gnustep experience.
> 
> Well, in a method I am using and re-using an object of
> NSMutableString, so I create it once with +stringWithCapacity, an than
> I initialize with -initWithFormat. Something like:
> 
>  NSMutableString *outmsg=[NSMutableString stringWithCapacity: 1000];
>  outmsg=[outmsg initWithFormat: @"first: %@", [arr objectAtIndex: 0]];
>  // use outmsg
>  outmsg=[outmsg initWithFormat: @"second: %@", [arr objectAtIndex: 1]];
>  // use outmsg
> outmsg=[outmsg initWithFormat: @"third: %@", [arr objectAtIndex: 2]];
>  // use outmsg
> 
> What I observe is that every time I invoke -initWithFormat the text is
> appended at the end of the existing string. Is it the right behavior
> to expect? Am I doing something wrong?

-init or -initWithFormat: should be called only once and only after +alloc.
You use stringWithCapacity which has already been initialized.

Use -setString:@"" followed by -appendWithFormat: instead.

-- hns



reply via email to

[Prev in Thread] Current Thread [Next in Thread]