hillegass challenge

Discussion in 'Mac Programming' started by jamesapp, Aug 16, 2008.

  1. jamesapp macrumors 6502a

    Joined:
    Mar 7, 2008
    #1
    working on a challenge from a book on Cocoa programming for Mac os x, by Hillegas. The challenge is to write an application that takes what the user types and copies the string, and then displays the string and the number of characters in the string. i started a new project in x-code and then in interface builder i dragged a button and a text field from the library window to my window for my application. i also dragged a label to my application window. That was the easy part. I want to have the label when my application runs, display three question marks. and then display a string which the user has entered into the textField, along with the string i want my application to display the length of the string.

    can i use printf or should i use NSLog, just trying to complete a challenge.

    i have an older book on objective-c i tried writing a simple program. I wanted the program to initially be a command line tool. I thought if i can get my computer to count the number of characters in a string, in a program that is a command line tool, that i could take what i learned from writing the program and apply it to the challenge from the Hillegas book. I don't know if that is the best way to solve the challenge? So my question is how do i count the number of characters in a string. In the older book on objective-c there is a program in it that has the following code snippet:
    Code:
    NSString *str1 = @"This is string A" ...
    
    //count the number of characters
    printf ("Length of str1: %i\n", [str1 length]);
    
    what is length in these lines, is it a method.

    in the Hillegas book:
    Code:
    "You will also find it useful to know about the following methods of the class NSString:
    
    - (int)length"
    
    how do i get what the user types in, and copy it, and or count it.
    How would you teach someone to complete this challenge? To paraphrase Denzel Washington "explain how i would get started writing this application as if i were a six year old." I would accept help with the x-code project as an application or as a command line tool.
    any help would be appreciated.
     
  2. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
    Got it to work, here is the code.

    CountChars.h
    Code:
    #import <Cocoa/Cocoa.h>
    #import <Foundation/Foundation.h>
    
    @interface CountChars : NSObject
    {
        IBOutlet NSTextField *labelOne;
        IBOutlet NSTextField *textBox;
    }
    
    -(IBAction)count:(id)sender;
    -(void)awakeFromNib;
    
    @end
    CountChars.m
    Code:
    #import "CountChars.h"
    
    @implementation CountChars
    
    -(IBAction)count:(id)sender
    {
        int numChars;
        NSString *textToPrint;
        NSString *enteredText;
        
        enteredText = [textBox stringValue];
        
        numChars = [enteredText length];
        
        textToPrint = [NSString stringWithFormat:@"'%@' has %i characters.", enteredText, numChars];
        [labelOne setStringValue:textToPrint];
    }
    
    -(void)awakeFromNib
    {
        NSString *label = @"???";
        
        [labelOne setObjectValue:label];
    }
    
    @end
    Edit : Don't forget to do the Interface Builder link ups. That caught me out for about 15 minutes wondering why nothing was happening :).
     
  3. WebMongol macrumors member

    Joined:
    Sep 19, 2004
    Location:
    Bay Area, CA
    #3
    Python:
    Code:
    #! /usr/bin/env python
    
    copy = None
    while copy != "quit":
        input = raw_input("Enter any string, 'quit' to finish: ")
        copy  = input[:]
        print "you entered: <%s>, length: %d" % (copy, len(copy))
    
    C (for brave souls):
    Code:
    #include <stdio.h>
    #include <string.h>
    
    const int MAXLEN = 4096;
        
    int main(int argc, char** argv) {
        char copy[MAXLEN];
        *copy = '\0';
    
        while (strcmp(copy, "quit") != 0) {
            char input[MAXLEN];
            printf("Enter any string, 'quit' to finish: ");
            if (!fgets(input, sizeof(input), stdin)) {
                break;
            }
            input[strlen(input)-1] = '\0';  /* chop trailing '\n' */
            strncpy(copy, input, sizeof(copy)-1);
            printf("you entered: <%s>, length: %d\n", copy, strlen(copy));
        }
        return 0;
    }
    
    
     
  4. jamesapp thread starter macrumors 6502a

    Joined:
    Mar 7, 2008
    #4
    having trouble getting a challenge to work.
    i used your CountChars.h interface file.
    I also used your CountChars.m implementation file.
    I think my problem is in Interface Builder. My application runs but all it does is print like the first three characters of the string that i input. I need help with Interface Builder like i have a text field a button and a label, i also have an object just wondering how to link them up. I think that is my problem. any help would be appreciated.
     
  5. LoveMyMac2004 macrumors newbie

    Joined:
    Aug 18, 2008
    Location:
    Myrtle Beach, SC
    #5
    Hi jamesapp,

    I am also going through the book. At this point if you have read through the book starting from chapter 1, you should be able to do this. Remember what he says in the book? "This is hard and you and NOT stupid."

    You only need 2 outlets and one action. Did you create the window that was shown in the book? If you did, then you have a button and 2 text fields. You do NOT need the awakeFromNIB at all.

    Start with your .h file like this:

    Code:
    #import <Cocoa/Cocoa.h>
    
    @interface AppController : NSObject 
    {
    	IBOutlet NSTextField *inputField;
    	IBOutlet NSTextField *outputField;
    }
    - (IBAction)countCharacters:(id)sender;
    @end
    
    Then all you need is to handle the single action like this:

    Code:
    #import "AppController.h"
    
    @implementation AppController
    - (IBAction)countCharacters:(id)sender
    {
            NSString *format = @"'%@' has %d characters"
            int characters = [[inputField stringValue] length];
    	[outputField setStringValue:[NSString stringWithFormat:format
                                                          ,[inputField stringValue]
                                                          ,characters]];
    }
    @end
    
    That is all you should have to do. The #import line should be whatever you called your class. In this case I named it AppController. I am already on chapter 11 in the book. I hope this helps. Also what version of XCode are you using and what OS version are you on? I had some trouble because the book is assuming you have 10.5 with XCode 3.1. The concepts are the same but screens and menus are different.

    If you need any further help, drop me a line.

    -Don
     
  6. jamesapp thread starter macrumors 6502a

    Joined:
    Mar 7, 2008
    #6
    i am confused on the linking in interface builder. like how do i know what to link with what? I have a text field a label and a button in Interface Builder, i also have an NSObject in interface builder, like when the user hits count characters which is my button, what does that button need to know about. like should it know about the NSObject which happens to be AppController? looking up AppController in the identity inspector it has two outlets and one action. just wondering what the logic of connecting them is? any help would be appreciated. In one of my versions of the string counting program (i have Count Chars instead of AppController,) in interface builder when i look at NSObject in the identity inspector, i have two outlets and one action. Count is an action and labelOne and textBox are two outlets just wondering how i would go about linking them in iterface builder. again any help would be appreciated.
     
  7. lucasgladding macrumors 6502

    Joined:
    Feb 16, 2007
    Location:
    Waterloo, Ontario
    #7

    Think of the CountChars controller as an "employee" doing all the work. The button issues the work request to the CountChars "employee". The CountChars "employee" then needs to know where to find the information he needs to count and where to write his answer.

    In practice: you will link from the count button to the controller for the count: action and link from the controller to the text box and label through the respective outlets. The button does not need to know anything, that's the job of the controller. You just need to tell the controller to start working.

    As I usually suggest, check out www.cocoadevcentral.com for tutorials when getting started. I learned Cocoa with the Hillegass books and Scott Stevenson's sites. It's amazing how far some nice illustrations go in understanding the concepts :) .
     
  8. jamesapp thread starter macrumors 6502a

    Joined:
    Mar 7, 2008
    #8
    still having trouble with the challenge. Count Chars is linked with what?
    i cntl clicked on Count Chars, but i didn't see anything to link?
    again i need help but don't want you to just give me the answer. Any help would be appreciated.
     
  9. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #9
    To be fair I'd suggest you reread the preceding chapter as it explains all the steps to take.
     

Share This Page