Re: Oddity
Re: Oddity
- Subject: Re: Oddity
- From: Esteban <email@hidden>
- Date: Fri, 16 Nov 2001 21:54:47 -0800
It seems to me you could do something liek the following
NSString *city = @"Los Angeles";
NSString *state = @"California";
NSString *zip = @"99999";
NSString *cityStateZip = @"";
cityStateZip = [cityStateZip stringByAppendingFormat:@"%@ %@ %@", city,
state, zip];
the %@ lets you replace a NSString value in place to the output
NSString, just like %d would let you replace a integer value in place to
the output of an NSString (or for that matter a C String if you were
using printf)
You can also use an NSMutableString to append more information to a
string, without having to create a new string with the appended
information.
with an NSMutableString you could do
NSString *city = @"Los Angeles";
NSString *state = @"California";
NSString *zip = @"99999";
NSMutableString *cityStateZip = [NSMutableString string];
[cityStateZip appendFormat:@"%@ %@ %@"", city, state, zip];
I think this looks cleaner :)
I hope that code answered your question a bit. It seems to be what you
are trying to do.
-Esteban
On Thursday, November 15, 2001, at 09:33 PM, email@hidden wrote:
I'm finishing up the first app I've done that's not a tutorial, a
simple mortgage calculator. I have a field named 'cityStateZip'. I fill
the field with a series of 'stringByAppendingString's. When I first
built this part of the routine, I found I couldn't append anything if
nothing was intially in the string. So I created the string with a
blank: NSString* cityStateZip = @" ". Not a solution I liked because
now I have blank spaces in front of my test. When I had the rest of the
program running, I went back to this code to try some other stuff. I
tried to initWithString:city and a couple of other things, that I
couldn't get to work. Anyway, it's getting late and I want the thing to
run before quitting for the night. I rewrite: NSString* cityStateZip =
@" ". It compiled fine. When I ran the program I got errors. (Unable to
access the stupid variable!). After a fair amount of head scratching, I
eventually copied the new iteration of the variable name and pasted
them over the other instances of it and now it works fine. I am
ABSOLUTELY postitive the new variable name was identical to the old
ones.
Any quesses as to what happened. (And while we're at it, how do I get
the first value, city, into the string without a leading space?)
_______________________________________________
cocoa-dev mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
References: | |
| >Oddity (From: email@hidden) |