Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

h2ospeedster7

macrumors newbie
Original poster
Apr 26, 2009
1
0
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?
 

ausername

macrumors newbie
Feb 28, 2009
25
0
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.
 

ausername

macrumors newbie
Feb 28, 2009
25
0
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, :)
 

fizy45

macrumors newbie
Jul 22, 2012
2
0
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....
 

jared_kipe

macrumors 68030
Dec 8, 2003
2,967
1
Seattle
Code:
- (IBAction)setTo5:(id)sender {
	int random_num;
	random_num = (arc4random() % 7 - 1) + 1;
	if (random_num == 1)
		[textField setStringValue:@"Hello1"];
	else if (random_num == 2)
		[textField setStringValue:@"Hello2"];
	else if (random_num == 3)
		[textField setStringValue:@"Hello3"];
	else if (random_num == 4)
		[textField setStringValue:@"Hello4"];
	else if (random_num == 5)
		[textField setStringValue:@"Hello5"];
	else if (random_num == 6)
		[textField setStringValue:@"Hello6"];
	else if (random_num == 7)
		[textField setStringValue:@"Hello7"];
		
}
@end
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.