PDA

View Full Version : Help -Noob exercise - App to count characters in a text field (hillegasse) - crashes




I'm a Mac
Jun 16, 2008, 05:45 PM
I am a programming noob, so please excuse my lack of experience. Hillegasse's challenge was to create an application that will count the amount of characters in a text field. But mine crashes- I can't really understand what's on the debugger (it just says that GNU has no warranty so here is my code (garbage collection is on)

AppController.h

#import <Cocoa/Cocoa.h>


@interface AppController : NSObject
{
IBOutlet NSTextField *input;
IBOutlet NSTextField *output;
}
-(IBAction)countIt:(id) sender;
@end


AppContoller.m

#import "AppController.h"

@implementation AppController

-(IBAction)countIt:(id)sender
{
NSString *string1 = [input stringValue];
NSString *string2 = [string1 length];

[output setStringValue:(@"%@", string2)];
}
@end


Attatched is the interface- or at least how I created it in Interface builder. I control dragged from the button to the AppController object, and then command-clicking the AppController objects, I connected it to the two textfields.

Thank you so much for your help.



Gelfin
Jun 16, 2008, 05:51 PM
The result of NSString's length method is not an NSString*.

gnasher729
Jun 16, 2008, 06:13 PM
#import "AppController.h"

@implementation AppController

-(IBAction)countIt:(id)sender
{
NSString *string1 = [input stringValue];
NSString *string2 = [string1 length];

[output setStringValue:(@"%@", string2)];
}
@end




The argument in the setStringValue call is most likely not what you think it is. Something like (x, y) is a "comma-expression": When you have two expressions, separated by comma, the first expression is evaluated, the result thrown away, then the second expression is evaluated and that is the value that is used. I suppose there is a "stringWithFormat" missing somewhere.

I'm a Mac
Jun 16, 2008, 06:33 PM
so what do I do to fix it

laprej
Jun 16, 2008, 07:21 PM
bar = [str length];

nstr = [NSString stringWithFormat:@"%@ has %d letters.", str, bar];


bar is an int, nstr is an NSString*.

Good luck.

HawaiiMacAddict
Jun 16, 2008, 07:30 PM
Aloha I'm a Mac,

I've completed that particular challenge, but can't remember the code off the top of my head. I'll check once I get home from work to let you know what I did (or at least point you in the right direction).

:apple:HawaiiMacAddict

NexesDev
Jun 16, 2008, 08:09 PM
Use this, it will work.

AppController.h

#import <Cocoa/Cocoa.h>


@interface AppController : NSObject
{
IBOutlet NSTextField *input;
IBOutlet NSTextField *output;
}

-(IBAction)checkIt:(NSButton *)sender;

@end


AppController.m

#import "AppController.h"


@implementation AppController

-(IBAction)checkIt:(NSButton *)sender
{
NSString *string = [input stringValue];
NSInteger count = [string length];

[output setIntValue: count];
}

@end

http://att.macrumors.com/attachment.php?attachmentid=120245&stc=1&d=1213664878

I'm a Mac
Jun 16, 2008, 08:16 PM
I did it--- yay me ! [Not, by myself of course, but with a little help from my friends]. Anyway, guys thanks, for your help- and not just giving me the answer. I now understand that string1 length is an integer, and in order for me to get the textfield to display the amount of characters [an integer value], I had to use the method setIntValue.

Edit: I posted this before I saw your solution. Mine happens to be a little bit different. (Did you set your connections with interface builder or programmatically?) Here is my code.
Also, how to you get NSTextField to recongnize the stringWithFormat method?

#import "AppController.h"


@implementation AppController

-(IBAction)countIt:(id)sender
{
NSString *string1 = [input stringValue];
int count;
count = [string1 length];

[output setIntValue: count];
}
@end

I'm a Mac
Jun 16, 2008, 09:24 PM
How to I get the output to display as: 'This' has 4 characters. - as opposed to just 4. Here is my new code.


#import "AppController.h"


@implementation AppController

-(IBAction)countIt:(id)sender
{
NSString *string1 = [input stringValue];
NSInteger count;
count = [string1 length];
[output stringWithFormat:@"%@ has %d characters.", string1, count];
}
@end


Attached is the debugger with the warnings and the error.

NexesDev
Jun 16, 2008, 11:15 PM
Remember that stringWithFormat is a class method, so when sending your message you need to call the class, in this case it is NSString. So change your .m file to look like this.

NSString *string = [input stringValue];
NSInteger count = [string length];
NSString *display = [NSString stringWithFormat:@"The number of letters are %i", count];

[output setStringValue:display];

and you should get something like this. I hope this helps.

HawaiiMacAddict
Jun 17, 2008, 12:03 AM
Aloha everyone,

Your solutions seem so much more elegant than mine. I have only been coding with Obj-C since I got Aaron Hillegass' book, however :D Anyway, here's how I solved the challenge.

On my .xib file, I have two text fields (one is a label) - textField (for user input) and textField2 (the label showing the results of the app) - and one button. I've included screenshots of the connections to my object, my .h and .m files, and the window both before and after one run. My code is a bit longer than what's been offered - I guess part of being a good coder is achieving the same results with less code.

HawaiiMacAddict

I'm a Mac
Jun 17, 2008, 06:48 AM
Thank you all for your solutions.... but can you explain the difference between class and instance methods (or in this case, the stringWithFormat method?) Since it's a class method, you had to initialize a "special" instance of a string, right? But why? Is it because there is no instance method stringWithFormat?

white89gt
Jun 17, 2008, 08:27 AM
Here's the best way that I can think of to explain the difference between instance methods and class methods. An example of an instance method is length. You call count on the particular instance of an NSString to find out how many characters are in it.
int i = [myString length];

A class method is usually something that returns a initialized instance of a class. In the context of your question, the stringWithFormat method returns an initialized NSString with the format you specify. stringWithFormat would make more sense to beginners if it was called initStringWithFormat.
NSString *myString = [NSString stringWithFormat:@"%i", [myOtherString length]];

If you look through the documentation, it's easy to pick out the class methods because their methods all start with a + instead of a -.

Also, not that it makes much difference, but I suck at retain/releases so I tend to nest my coding where ever possible (Hillegas mentions this very early in the book, but doesn't use it for the most part). So instead of doing something like this:
finalCount = [NSString stringWithFormat:@"%@ has %i characters.", string, count];
[textField2 setObjectValue:finalCount];
I would do something more along the lines of this:
[textField2 setStringValue:[NSString stringWithFormat:@"%@ has %i characters." [textField1 stringValue], [textField1 count]]];

I'm a Mac
Jun 17, 2008, 11:16 AM
So in order to create a string that uses the string in the textfield, and the integer count of the textfield, you must initialize that string first before you set the output to display the results. But why must you use setObjectValue instead of setStringValue? Also, would this work in NSTextView- would NSTextView count as an instance of NSTextfield? Also, how would I count words instead of characters?

white89gt
Jun 17, 2008, 01:46 PM
So in order to create a string that uses the string in the textfield, and the integer count of the textfield, you must initialize that string first before you set the output to display the results. But why must you use setObjectValue instead of setStringValue? Also, would this work in NSTextView- would NSTextView count as an instance of NSTextfield? Also, how would I count words instead of characters?

I didn't realize it said setObjectValue everywhere; setStringValue is correct for one of them...setObjectValue is right for the others. If you're creating a string and then setting another string equal to it, setObjectValue makes one object equal to the other. setStringValue sets the string value of the NSString * equal to a particular character string. Anyway, I fixed my earlier post so that all the statements are correct. As for the other questions, NSTextView can't be an instance of NSTextField. However, they do both inherit from the NSText parent class, so it just depends on where the count method is implemented. I'd be willing to bet that NSTextView has the same method, but I don't know for sure. I also have no idea about counting words, but I'm sure it can be done.

illscientifix
Jun 29, 2010, 10:50 PM
i posted a download of a working version on my blog ( finally ) @ illscientifix.com - downloads (http://illscientifix.com/?page_id=15) thanks to this thread , it really sux being a noob, really.. so i hope i can help out the next joe-noob who may come accross this thread with a working example , IB can be a bit confusing at first..

THANK YOU MACRUMORS.COM!

chrono1081
Jun 30, 2010, 01:49 AM
Cocoa is challenging. I came over from years of C++, and C and learned Objective-C then hopped to Cocoa.

The Hillegass book is nice and challenging, but if you are really new you may get stuck and super frustrated at some of it.

You may want to have a look at one of the fruit books to compliment it. If you get stuck in Hillegass's book, hope over to the fruit book and have a look at how they did the same thing.

Here is the fruit book for Cocoa: http://apress.com/book/view/1430218592

I used both hand in hand and it worked great. (I'm still no expert by any means but I'm getting a lot better and have a game halfway built :D )