Re: I don't get how to cast an integer to string
Re: I don't get how to cast an integer to string
- Subject: Re: I don't get how to cast an integer to string
- From: Nicholas Riley <email@hidden>
- Date: Wed, 22 May 2002 07:50:18 -0500
- Mail-followup-to: Ted Petrosky <email@hidden>, email@hidden
On Wed, May 22, 2002 at 08:35:10AM -0400, Ted Petrosky wrote:
>
how can I cast the integer to string:
>
>
int j=65;
>
>
NSMutableString *aFormattedString = [[NSMutableString alloc]
>
initWithFormat:@"There are rows"];
>
>
[aFormattedString insertString:[j stringValue] atIndex:10];
>
>
I am just learning how to RTM.
OK, a few things you should learn before you go further:
- the difference between NSMutableString and NSString. If you don't
need to change a string after creating it, don't use
NSMutableString.
- the difference between a primitive type (int) and an object
(NSMutableString). You can't send a message (like 'stringValue') to
something that isn't an object.
- how to look up the methods that apply to a given class of object,
either with the header file or documentation. There's a short
QuickTime video on Apple's developer site which shows you how to use
the documentation lookup features in Project Builder If you had
known this, you would have figured out that you can't send
stringValue to an int.
- how to use initWithFormat: (start with the man page for printf, then
look at the Objective-C additions, mainly %@)
Most of this is covered adequately by the Cocoa reference
documentation. An introductory Cocoa book would probably provide a
more gentle introduction though (see the archives of this list for
plenty of recommendations).
Since you have some idea how to use format strings in C,
initWithFormat should be similar for you. What you want is this:
NSString *aFormattedString = [NSString stringWithFormat: @"There are
%d rows", j];
or if you want a retained version:
NSString *aFormattedString = [[NSString alloc] initWithFormat: @"There
are %d rows", j]];
and if you don't know the difference between the two lines of code
above, read some documentation on Cocoa memory management.
Another recommendation I'd make while learning the Cocoa APIs
(especially Foundation) is to use a program like F-Script or Joy which
lets you experiment with them interactively, instead of having to
compile/run programs.
--
=Nicholas Riley <email@hidden> | <
http://www.uiuc.edu/ph/www/njriley>
Pablo Research Group, Department of Computer Science and
Medical Scholars Program, University of Illinois at Urbana-Champaign
_______________________________________________
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.