Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-01-2009, 11:38 AM   #1 (permalink)
New Member
 
Join Date: Apr 2009
Posts: 36
julesv is on a distinguished road
Default WTFunk arc4randomness

I can define a random integer as such:

int variable = arc4random()%5-1;

This works in my viewDidLoad method. But when I try and call in it in an action called by a button, it acts like it doesn't know what it is. And yeas I did try make pointers via the viewDidLoad method as such:

int *variablePointer = &variable;

Pleeease help...
julesv is offline   Reply With Quote
Old 05-01-2009, 11:43 AM   #2 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
FlyingDiver will become famous soon enough
Default

Quote:
Originally Posted by julesv View Post
I can define a random integer as such:

int variable = arc4random()%5-1;

This works in my viewDidLoad method. But when I try and call in it in an action called by a button, it acts like it doesn't know what it is. And yeas I did try make pointers via the viewDidLoad method as such:

int *variablePointer = &variable;

Pleeease help...
I'm not sure what pointers have to do with this function.

You're calling arc4random in some method and it doesn't work? Or you're trying to access the random number you generated in viewDidLoad, and that doesn't work? It's not clear from your posting.

joe
FlyingDiver is offline   Reply With Quote
Old 05-01-2009, 11:47 AM   #3 (permalink)
New Member
 
Join Date: Apr 2009
Posts: 36
julesv is on a distinguished road
Default

Quote:
Originally Posted by FlyingDiver View Post
I'm not sure what pointers have to do with this function.

You're calling arc4random in some method and it doesn't work? Or you're trying to access the random number you generated in viewDidLoad, and that doesn't work? It's not clear from your posting.

joe
I'm sorry, I'm just a bit confused myself lol. I'm trying to access the random value generated in viewDidLoad in an action called by a button.
julesv is offline   Reply With Quote
Old 05-01-2009, 11:49 AM   #4 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
FlyingDiver will become famous soon enough
Default

Quote:
Originally Posted by julesv View Post
I'm sorry, I'm just a bit confused myself lol. I'm trying to access the random value generated in viewDidLoad in an action called by a button.
Ok. Is that an instance variable using @property & @synthesize? Or did you try to make it a global variable? And what does the code look like that you're using to access it?

joe
FlyingDiver is offline   Reply With Quote
Old 05-01-2009, 11:58 AM   #5 (permalink)
New Member
 
Join Date: Apr 2009
Posts: 36
julesv is on a distinguished road
Default

No, it's not @synthesize'd I'm not sure how to make it a global, I think that could help, I just put in viewDidLoad:

int variable = 10;
int *variablePointer = &variable;

Then in the action called by the button, I put

if(*variablePointer [error] == otherVariable) {
label.text = @"hello";
}
julesv is offline   Reply With Quote
Old 05-01-2009, 12:14 PM   #6 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Quote:
Originally Posted by julesv View Post
I can define a random integer as such:

int variable = arc4random()%5-1;

This works in my viewDidLoad method. But when I try and call in it in an action called by a button, it acts like it doesn't know what it is. And yeas I did try make pointers via the viewDidLoad method as such:

int *variablePointer = &variable;

Pleeease help...
I don't think this is a "random" problem, I think it's a problem with variable scope. If you declare a variable inside a method, it can only be used inside that method. If you want it to be used in the whole class, DECLARE it in your .h file, between the brackets of the @interface section. Then you can SET it inside the viewDidLoad method, and read it from anywhere in the class.

If you need to read it from another class, then you need a property (or a method that returns it.)

Code:
//this is a declaration - do this in your .h file
int myVariable;

//this is setting the variable - do this in a method
myVariable = 17;
__________________

Free Games!
smasher is offline   Reply With Quote
Old 05-01-2009, 12:29 PM   #7 (permalink)
New Member
 
Join Date: Apr 2009
Posts: 36
julesv is on a distinguished road
Default

Thank you so much! That's one thing more learnt today.
julesv is offline   Reply With Quote
Old 02-16-2011, 08:40 PM   #8 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 1
dches is on a distinguished road
Smile

hi,

so im trying to do what you said:

in my .h file i call my int variable like this:

Code:
int losingVaultNumber;
then in my .m in the viewdidload I create the number like this:

Code:
 losingVaultNumber=rand()%12+2;
how do I implement the losingVaultNumber into an "if" statement? I dont know what im doing wrong?


heres how im trying to implement it:

Code:
if ([losingVaultNumber = 2]) {
		vault2Title= [[NSString alloc]
					  initWithFormat:@"YOU LOSE!"];
thanks!
dches is offline   Reply With Quote
Old 02-16-2011, 09:11 PM   #9 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Welcom to the forum!

First, please start a new thread if you have question. Don't post your questions in threads started by others, especially since this one has been dead for two years.

As for your code, there are two problems with: if ([losingVaultNumber = 2])

1. There are no [] brackets around variables. Those are used for sending messages to objects.
2. = is used to assign a value to something, == is used for comparison.

So it should be: if (losingVaultNumber == 2)

Also, it's a better idea to use arc4random() instead of rand(), for various reasons which can be found by Googling.
baja_yu is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
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
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 308
8 members and 300 guests
blueorb, givensur, guusleijsten, jbro, mer10, n00b, SLIC, yomo710
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,880
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:03 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0