Re: (no subject)
Re: (no subject)
- Subject: Re: (no subject)
- From: Ken Thomases <email@hidden>
- Date: Sat, 3 Oct 2009 06:52:16 -0500
On Oct 3, 2009, at 6:32 AM, Jos Timanta Tarigan wrote:
im trying to return a string without making any additional string
variable before. so in a method that return NSString*, i only put
this line. The first [self name] return a NSString* so its ok. but
[self angleInDegrees] returns a float and it return an error when i
try to do it with this statement:
return@"Im a %@", [selfname], " with angles %d", [selfangleInDegrees];
any1 have any idea what went wrong?
That's completely not how you do what you're attempting to do.
Strings don't automatically format themselves by being listed with
commas between them.
The comma operator of C (and thus Objective-C) is a bit unusual. It
means that each subexpression should be evaluated in turn, but the
value of the overall expression is just the value of the last
subexpression in the series. So, the above is essentially the same as:
@"Im a %@"; // statement consisting of an expression. Since there
are no side effects, it does nothing
[self name]; // statement invoking -name on self; any return value
from -name is ignored
" with angles %d"; // same as first
return [self angleInDegrees];
In short, you don't want to use the comma operator; it doesn't behave
how you think it does.
What you seem to have wanted is something like this:
return [NSString stringWithFormat:@"Im a %@ with angles %f", [self
name], [self angleInDegrees]];
So, you have to explicitly invoke a method that performs formatting --
it doesn't just happen because you want it to. Second, the format
string is one string, followed by the values to be formatted. You
don't interleave the format string and the values.
I used the %f specifier, since you said that -angleInDegrees returns a
float. If you want to round it to an int, you should do that
explicitly, in which case it would be appropriate to use %d.
Regards,
Ken
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
References: | |
| >(no subject) (From: Jos Timanta Tarigan <email@hidden>) |