 |
 |
NSTextField: simple question
|
 |
|
 |
|
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status:
Offline
|
|
I am novice programmer, just starting to learn Obj-C, please excuse any incorrect use of jargon. Here are my questions regarding NSTextFields:
1. How do insert characters in between variables (like floats or ints)? For example: I want "23:12" to appear in the textfield (w/o the quotes) where 23 and 12 are two different ints. How do I get the ":" to appear in between them? I know how to do it in java, but not Obj-C.
2. Also, how do I format floats or ints to cut off decimals. Say I have a float that is 12.345, how would I get just, 12.3 to be displayed?
Any suggestions and sample code greatly appreciated. Please understand that I am a beginner struggling with some basic tasks. Thanks in advance.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Eagan, MN
Status:
Offline
|
|
For number 1: Say hours and minutes are ints:
NSString output = [[NSString alloc] initWithString:[[NSNumber numberFromInt:hours] stringValue]];
output = [output stringByAppendingString:@":"];
output = [output stringByAppendingString:[[NSNumber numberFromInt:minutes] stringValue]];
[someTextField setStringValue :output];
[output release];
There is a MUCH faster way of doing this by using [NSString initWithFormat:] but I've never attempted this.
For number 2: Use [NSString initWithFormat:]. Once again, I'm not sure exactly how. But, I would GUESS it would go something like this:
NSString output = [[NSString alloc] initWithFormat:@"%.3d", someInt];
------------------
[This message has been edited by mr_sonicblue (edited 05-29-2001).]
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status:
Offline
|
|
Thank you. That is VERY helpful.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status:
Offline
|
|
[QUOTE]Originally posted by mr_sonicblue:
[B]For number 1: Say hours and minutes are ints:
NSString output = [[NSString alloc] initWithString:[[NSNumber numberFromInt:hours] stringValue]];
output = [output stringByAppendingString:@":"];
output = [output stringByAppendingString:[[NSNumber numberFromInt:minutes] stringValue]];
[someTextField setStringValue  utput];
[output release];
I used this code (except changed int names as appropriately) and I get the following errors:
illegal statement, missing ';' after 'NSString'
parse error before 'output'
cannot find class (factory) method.
return type for 'numberFromInt:' defaults to id
_______
Unfortunately, being such a Newbie, I am not sure what these errors mean.
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status:
Offline
|
|
For 1):
NSString *output = [NSString stringWithFormat:@"%02d:%02d", hours, minutes];
[textField setStringValue:output];
If you already have an NSCalendarDate instance, you can use the -descriptionWithCalendarFormat: method there (which uses different format tokens -- look at the NSCalendarDate documentation).
For 2), as mr_sonicblue said:
NSString *output = [NSString stringWithFormat:@"%.1f", floatValue];
[textField setStringValue:output];
will round the value to one decimal place. %.2f will round to two decimal places, etc.
See the printf(3) man page for a full description of all the formatting options you can use with printf() and with NSString's +stringWithFormat:/-initWithFormat:.
Notes on the other code -- NSNumber's -stringValue already returns an autoreleased NSString instance, so there's no need to create another copy. Thus the following would also work:
NSString *output = [[NSNumber numberWithInt:hours] stringValue];
output = [output stringByAppendingString:@":"];
output = [output stringByAppendingString:[[NSNumber numberFromInt:minutes] stringValue]];
[someTextField setStringValue:output];
It wouldn't put a leading 0 for single-digit values though -- that's the difference between %02d and %d in the formats.
As far as the errors, the first one was because the variable was declared "NSString output" instead of "NSString *output" (note the asterisk), and the second was because either #import <Foundation/NSValue.h> was missing (meaning the compiler doesn't know about any of NSNumber's methods), or more likely because the method is called +numberWithInt: not +numberFromInt:.
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Mar 2001
Status:
Offline
|
|
Here's the smallest (as in least code required) way to get the colon to appear in an NSTextField for integers:
[myTextField setStringValue:[NSString localizedStringWithFormat:@"%d:%d", firstInt, secondInt]];
Note that if you're trying to display the time in a text field, you'd be better off with an NSDateFormatter. It's easiest to set those up in IB; feel free to ask for more help if that's what you want to do.
The proper way to control the number of decimal places displayed in an NSTextField is to use an NSNumberFormatter. In brief: instantiate an NSNumberFormatter in IB, hook up the NSTextField's formatter outlet to it, and then configure the NSNumberFormatter to display the number of decimal places you want via the inspector.
Hope this helped.
-Peter
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status:
Offline
|
|
lindberg,
I tried your code with appropriate changes in variable names:
NSString *output = [NSString stringWithFormat:@"%02d:%02d", hours, minutes];
[textField setStringValue  utput];
[output release];
________
I get the following error for the first line:
parse error before '*'
Any suggestions? Do I need to import anything or something?
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status:
Offline
|
|
[QUOTE]Originally posted by Wixar:
[B]Here's the smallest (as in least code required) way to get the colon to appear in an NSTextField for integers:
[myTextField setStringValue:[NSString localizedStringWithFormat:@"%d:%d", firstInt, secondInt]];
This worked great! Thank you...
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status:
Offline
|
|
Unlike Java, you can only declare variables at the beginning of a block (i.e., after the "{"). So, put
NSString *output;
at the top of the block, and put
output = [NSString stringWithFormat:...]
later on when you get to your previous code. Or, just do it Wixar's way, which doesn't use a local variable :-)
As has been noted in other threads, the recent C99 spec changes C to allow variable declarations intermixed with code (like Java), so at some point down the road this situation will change. For now though, you'll have to follow traditional C rules on this.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Sep 2000
Location: Eagan, MN
Status:
Offline
|
|
Originally posted by Wixar:
Here's the smallest (as in least code required) way to get the colon to appear in an NSTextField for integers:
[myTextField setStringValue:[NSString localizedStringWithFormat:@"%d:%d", firstInt, secondInt]];
Thanks Wixar, that's what I was talking about
------------------
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|