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 09-26-2011, 07:52 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 15
Killbill is on a distinguished road
Default Random floats

Hi,
I'm trying to make an IBAction pick a random float from a fixed list (1.45, 3.89, 5.77 and 12.56).

In the viewDidLoad:

pickNumber = [[NSMutableArray alloc] init];
[pickNumber addObject:@"1.45"];
[pickNumber addObject:@"3.89"];
[pickNumber addObject:@"5.77"];
[pickNumber addObject:@"12.56"];

In the IBAction:
-(IBAction)butoonPressed: (id)sender{
result = [pickNumber objectAtIndexarc4random() % [pickNumber count])];

Obviously this isn't working, i get an error "Assigning to 'float' from incompatible type 'id'.
Any ideas how to get one of the numbers into "result"?

Last edited by Killbill; 09-26-2011 at 07:56 AM.
Killbill is offline   Reply With Quote
Old 09-26-2011, 08:39 AM   #2 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,549
RLScott is on a distinguished road
Default

Quote:
Originally Posted by Killbill View Post
Hi,
I'm trying to make an IBAction pick a random float from a fixed list (1.45, 3.89, 5.77 and 12.56).

In the viewDidLoad:

pickNumber = [[NSMutableArray alloc] init];
[pickNumber addObject:@"1.45"];
[pickNumber addObject:@"3.89"];
[pickNumber addObject:@"5.77"];
[pickNumber addObject:@"12.56"];

In the IBAction:
-(IBAction)butoonPressed: (id)sender{
result = [pickNumber objectAtIndexarc4random() % [pickNumber count])];

Obviously this isn't working, i get an error "Assigning to 'float' from incompatible type 'id'.
Any ideas how to get one of the numbers into "result"?
The objects you are adding to pickNumber are not floats or any other kind of numbers. They are strings. Look up the NSNumber class and how to use it.
RLScott is offline   Reply With Quote
Old 09-26-2011, 09:02 AM   #3 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 15
Killbill is on a distinguished road
Default

I am finding the documentation really difficult to apply...
Killbill is offline   Reply With Quote
Old 09-26-2011, 11:30 AM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Killbill View Post
I am finding the documentation really difficult to apply...
Look in the docs for NSNumber. Specifically, take a look at the methods numberWithFloat / numberWithDouble, and floatValue.

You could do this:


Code:
[myArray addObject: [NSNumber numberWithFloat: 10.3]];
[myArray addObject: [NSNumber numberWithFloat: 1.23]];
[myArray addObject: [NSNumber numberWithFloat: 2.47]];
Then, when you extract an object from the array, use code like this:

Code:
int randomIndex = arc4random()% [myArray count];
NSNumber* numberFromArray = [myArray objectAtIndex: randomIndex];
float value = [numberFromArray floatValue];
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 09-26-2011, 03:16 PM   #5 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 149
crashmonkey is on a distinguished road
Default

It might be simpler to use a C array instead of objective C arrays and objects.
Then you don't have to mess with converting to NSNumber.

I usually use C arrays whenever I am dealing with primitive variables like ints and floats.
__________________
Learning IOS development one crash at a time.
crashmonkey is offline   Reply With Quote
Reply

Bookmarks

Tags
nsmutablearray

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: 392
15 members and 377 guests
13dario13, 7twenty7, eski, EvilElf, glenn_sayers, HemiMG, iOS.Lover, jarv, n00b, pbart, Pudding, sacha1996, Sami Gh, UMAD, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,672
Threads: 94,121
Posts: 402,905
Top Poster: BrianSlick (7,990)
Welcome to our newest member, yuncarl28
Powered by vBadvanced CMPS v3.1.0

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