length and NSString? - MacRumors Forums
Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 11, 2008, 08:37 PM   #1
zmttoxics
macrumors 65816
 
zmttoxics's Avatar
 
Join Date: May 2008
length and NSString?

I am still learning Cocoa and Objective-C. I am trying to do an exorcise in the book I have (Cocoa Programming for Mac OS X 3rd Ed. By: Aaron Hillegass), and I can't figure it out.

I am supposed to make an application with an input text feild, a label, and a button. It's supposed to set the label the length of the text in the input box when you hit the button. I believe I have the app laid out correctly. However, I am not sure how to use the length method of the NSString class.

Code:
@interface count : NSObject {
	//input box
	IBOutlet NSTextField *textInput;
	//output label
	IBOutlet NSTextField *textOutput;
}
- (IBAction)count:(id)sender;
@end
Quote:
@implementation count
- (id)init
{
[super init];
NSLog(@"init");
return self;
}
- (IBAction)countid)sender
{
//This part is wrong, I know
[textOutput setStringValue:[textInput stringValue]];
NSLog(@"Counting...");
}
@end
The buttons and such are linked (as far as I can tell, if there is something you want me to check I can). But how can I set the output label to length of the input textfield?

Thanks!
__________________
iMac 20" 2.66 C2D|9400M|4GB RAM|320GB HD ;; MacBook Pro 15" 2.4 Ci5|GT330M|4GB RAM|320GB HD
HP w2558hc 25.5" - not quite an ACD, but damn awesome.
zmttoxics is offline   0 Reply With Quote
Old Aug 11, 2008, 08:55 PM   #2
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
NSString instances respond to -length which returns an int. There is also an initializer for NSString -initWithFormat that takes an NSString format string, say, @"%d", and a variable argument list, in this case one int. Now you have an NSString with the representation of your length.

Brought to you by the NSString class reference. I'd link it, but the iPhone has no copy-paste.

-Lee
lee1210 is offline   0 Reply With Quote
Old Aug 11, 2008, 09:19 PM   #3
TomM
macrumors newbie
 
Join Date: Dec 2007
The code:

Following is code which shows the typecasting:

<code>
- (IBAction)countid)sender
{
int numLtrs;

numLtrs = [[textField1 stringValue] length];
NSString *string = [NSString stringWithFormat:
@"%@ has %i letters.", [textField1 stringValue], numLtrs];

[textField2 setStringValue: string];
//[string release]; <-- Releasing string here causes an error
}
</code>

This works ok.

TomM
TomM is offline   0 Reply With Quote
Old Aug 12, 2008, 08:00 AM   #4
zmttoxics
Thread Starter
macrumors 65816
 
zmttoxics's Avatar
 
Join Date: May 2008
Ah, thanks guys!

I got it the way I wanted with:
Code:
[textOutput setIntValue:[[textInput stringValue] length]];
I am a very hardcore C programmer, and though I do a lot of C++ and java, this Object-C is giving me troubles with this method calls and interface linking.

Thanks for the help, I am sure I will be back!
__________________
iMac 20" 2.66 C2D|9400M|4GB RAM|320GB HD ;; MacBook Pro 15" 2.4 Ci5|GT330M|4GB RAM|320GB HD
HP w2558hc 25.5" - not quite an ACD, but damn awesome.
zmttoxics is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
php mysql = NSString IDMah iPhone/iPad Programming 9 Dec 20, 2013 01:35 PM
C Function from NSString? ArtOfWarfare Mac Programming 20 Jun 16, 2013 10:55 AM
NSString to NSSize larswik Mac Programming 1 May 28, 2013 06:54 PM
Why can't convert NSString to NSDate? mikezang iPhone/iPad Programming 4 Jan 22, 2013 06:56 PM
(NSString*) ?? thedon1 iPhone/iPad Programming 6 Jul 2, 2012 07:52 PM

Forum Jump

All times are GMT -5. The time now is 04:36 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC