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 04-07-2011, 07:28 AM   #1 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 68
canit210 is on a distinguished road
Default Call Random in Different Method

Alrighty so here's the low-down.

I've got one method that sets an NSUInteger declared in the .h file to a random integer using arc4random().

Later on in the code (in a different method) I'd like to use that random integer for a comparison. However, every time it's going through the comparison it creates a new random integer rather than using the one that was already established earlier.

Can anybody help me out?
__________________
Ping Ping: An arcade-style game featuring accelerometer-based controls, an achievement system, and local high score board.
Ping Ping

AddTap: Educational application that puts a unique spin on simple addition problems, adding speed and accuracy to the equation.
AddTap
canit210 is offline   Reply With Quote
Old 04-07-2011, 08:28 AM   #2 (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

Create a property or a pure ivar for the number and use that to get the generated value, don't make a new one every time.
baja_yu is offline   Reply With Quote
Old 04-13-2011, 09:10 PM   #3 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 68
canit210 is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
Create a property or a pure ivar for the number and use that to get the generated value, don't make a new one every time.
Hi baja,

I appreciate your response but am still running into a bit of trouble. I read up a bit more on properties to make sure I understood them correctly, but as it turns out, I don't think I do. In my .h class I set the randoms to NSUIntegers and declared them as properties as seen below:

Code:
        NSUInteger		*randomTall;
	NSUInteger		*randomWide;
	NSUInteger		*randomYPosn;
	NSUInteger		*randomXPosn;

@property (nonatomic, retain) NSUInteger *randomTall;
@property (nonatomic, retain) NSUInteger *randomWide;
@property (nonatomic, retain) NSUInteger *randomYPosn;
@property (nonatomic, retain) NSUInteger *randomXPosn;
In the .m file I synthesized the variables and used the following implementation to actually set the values in one of my methods:

Code:
randomTall = 50 + arc4random() % 197];
randomYPosn = 44 + arc4random() % (290-44-randomTall);
randomWide = 50 + arc4random() % 431; 	
randomXPosn = arc4random() % (480-randomWide);
In the .h file I am getting an error stating that the 4 properties with 'retain' attribute must be of object type... Should I change this to assign or is there a conflict between with using the NSUInteger type?

I am also getting an error in my .m file that says I am invalidly using the "- operand" because I'm using it with an int and NSUInteger type. Obviously conflicting.

One other big conflict I am seeing as well is that I am later using this random number to set the frame of a rectangle and because CGRectMake doesn't take an NSUInteger it's throwing an error here as well.

I think the biggest confusion is that I am not using the right data type and/or I may not be setting/assigning the property correctly. Once again, I assure that I did read some of the property tutorials posted here on iphoneDevSDK and other places online and made changes accordingly but I'm thinking a may still be pretty far off base.

Thanks so much for your help.
__________________
Ping Ping: An arcade-style game featuring accelerometer-based controls, an achievement system, and local high score board.
Ping Ping

AddTap: Educational application that puts a unique spin on simple addition problems, adding speed and accuracy to the equation.
AddTap
canit210 is offline   Reply With Quote
Old 04-13-2011, 10:07 PM   #4 (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

unsigned integers aren't objects nor can they be retained. You shouldn't be declaring them as pointers nor as retainer properties.

Also, doing this

randomTall = 50 + arc4random() % 197];

is accessing the variable directly and not using the setter generated by the property. If you were doing that with an object you would be in for some memory leaks. You need to use 'self' to use the property.
baja_yu is offline   Reply With Quote
Old 04-14-2011, 08:04 AM   #5 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 68
canit210 is on a distinguished road
Default

Finally got it working.

Thanks again baja
__________________
Ping Ping: An arcade-style game featuring accelerometer-based controls, an achievement system, and local high score board.
Ping Ping

AddTap: Educational application that puts a unique spin on simple addition problems, adding speed and accuracy to the equation.
AddTap
canit210 is offline   Reply With Quote
Reply

Bookmarks

Tags
method, random

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: 363
12 members and 351 guests
dansparrow, dre, ilmman, LezB44, michelle, Nobbsy, Objective Zero, samdanielblr, Sami Gh, shagor012, thephotographer, tinamm64
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,119
Posts: 402,896
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

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