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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
NSString from bool array BlobbleGame iPhone/iPad Programming 5 Jun 25, 2011 04:29 PM
PLEASE HELP!! making an iMovie video, length and recording volcom1151 Digital Video 2 Apr 14, 2010 06:50 PM
Help with unused variable and NSString may not respond ericb8272 iPhone/iPad Programming 5 Apr 4, 2010 05:40 PM
Convert Hex to string and NSString to NSData. Sui Jen iPhone/iPad Programming 0 Oct 14, 2009 10:34 PM
UITextField and NSString don't like spaces? ukdmbfan iPhone/iPad Programming 1 Feb 23, 2009 01:05 PM


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

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

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