Xcode random sting generator. Help with Obj-C!

Discussion in 'Mac Programming' started by h2ospeedster7, Apr 26, 2009.

  1. h2ospeedster7 macrumors newbie

    Joined:
    Apr 26, 2009
    #1
    Hello,

    I am currently quite new to Objective C and Xcode.
    I am trying to write a simple code for an applicaiton, that generates a random string of text (a fact) from an array.
    I was able to do this in Javascript, but not in Xcode.
    I then looked at an If function technique from this code, but I am now stuck on why it doesn't work.

    RUFacts.h
    #import <Cocoa/Cocoa.h>

    @interface RUFacts : NSObject {

    IBOutlet textField;
    }
    - (IBAction)reset:(id)sender;
    - (IBAction)set5:(id)sender;
    @end

    RUFacts.m
    #import "RUFacts.h"
    @implementation RUFacts

    - (IBAction)reset:(id)sender {
    [textField setIntValue:0];

    }

    - (IBAction)setTo5:(id)sender {


    int random_num;
    random_num = (random() % 10) + 1;
    [textField setIntValue:random_num];

    if random_num = 1
    [textField setStringValue:"Hello"];
    if random_num = 2
    [textField setStringValue:"Hello"];
    if random_num = 2
    [textField setStringValue:"Hello"];
    if random_num = 2
    [textField setStringValue:"Hello"];
    if random_num = 2
    [textField setStringValue:"Hello"];
    if random_num = 2
    [textField setStringValue:"Hello"];
    if random_num = 2
    [textField setStringValue:"Hello"];
    else


    }
    @end


    Any feedback, comments or suggestions would be greatly appreciated.
    Do you have any ideas of how I could do what I am trying to achieve?
     
  2. ausername macrumors newbie

    Joined:
    Feb 28, 2009
    #2
    Well for one, you're IBOutlet should look like this:

    IBOutlet NSTextField *myTextField;

    and once you do that make sure you connect the text field from interface builder to it. If you need help doing that, I can tell you how.
     
  3. ausername macrumors newbie

    Joined:
    Feb 28, 2009
    #3
    Here you go

    #import <Cocoa/Cocoa.h>


    @interface RUFacts : NSObject {
    IBOutlet NSTextField *textField;
    }

    - (IBAction)reset:(id)sender;
    - (IBAction)setTo5:(id)sender;

    @end

    #import "RUFacts.h"
    @implementation RUFacts
    - (IBAction)reset:(id)sender {
    [textField setIntValue:0];

    }

    - (IBAction)setTo5:(id)sender {
    int random_num;
    random_num = (arc4random() % 7 - 1) + 1;
    if (random_num == 1)
    [textField setStringValue:mad:"Hello1"];
    else if (random_num == 2)
    [textField setStringValue:mad:"Hello2"];
    else if (random_num == 3)
    [textField setStringValue:mad:"Hello3"];
    else if (random_num == 4)
    [textField setStringValue:mad:"Hello4"];
    else if (random_num == 5)
    [textField setStringValue:mad:"Hello5"];
    else if (random_num == 6)
    [textField setStringValue:mad:"Hello6"];
    else if (random_num == 7)
    [textField setStringValue:mad:"Hello7"];

    }
    @end


    Okay, use this code here, and build it. Then open you're interface file, drag out the text field, and the two buttons... Then drag out an object to your mainMenu.xib window (the window that contains "File's owner, First Responder"...etc. Once you have dragged an object, open the "Identity Inspector," (make sure the object is selected) and in the class field type "RUFacts" then hit the return key. After you've done this you can right-click and drag from the object (it's name will be Facts now) to the text field, and select "mytextField." Now the text field is hooked up. Do the same for the buttons, but this time, drag from the button to the object. Tell me if you get it working, :)
     
  4. fizy45 macrumors newbie

    Joined:
    Jul 22, 2012
    #4
    Hey I know this topic was old but I found it on internet I tried it with adding the code on the last post it worked !!

    But I need same code for ios development I tried to change it but failed please somebody help me with this code.

    Same code but for ios pleasee....
     
  5. Red Menace macrumors 6502

    Red Menace

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #5
    Try a Google search for UITextField, and good luck with your homework.
     
  6. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #6
    This has a subtle bug in it. (arc4random % 7) provides a number 0<=x<=6.
    (arc4random % 7 - 1) provides a number -1<=x<=5
    ((arc4random % 7 - 1) + 1) provides a number 0<=x<=6
    Your if/elseIf blocks do not test for 0 and will never hit 7.

    You could make it 'safer' by having an else (which would at least hit the 0), also using a switch statement would make it more compact and IMHO more readable.
     

Share This Page