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 01-27-2009, 10:20 AM   #1 (permalink)
Registered Member
 
Join Date: Jan 2009
Location: Leeds, England
Posts: 115
BenODwyer is on a distinguished road
Default Problems with Random numbers

Hi All

This is a bit of a noob question as I am new to Objective C, Cocoa and the iPhone SDK.

The outcome of this is to load a movie file. I want it to pick a movie at random from several named movie0.m4v to movie3.m4v

I generated a random number using:

Quote:
int r = arc4random() % 4;
Then I use "r" to append to the movie name like this:

Quote:
NSString *moviePath = [bundle pathForResource:@"movie"+r ofType:@"m4v"];
This doesn't work..

If I remove the reference to "r". Then rename one of my movies to "movie.m4v" it works a treat.

Can anyone shed some light on this for me.

Will be much appreciated.

Thanks.
BenODwyer is offline   Reply With Quote
Old 01-27-2009, 10:24 AM   #2 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 495
exorcyze is on a distinguished road
Default

[NSString stringWithFormat:@"movie%i", r]

=)
__________________
My Apps on AppStore : gScale (guitar scales reference), eMaze, eMaze Lite, eTimesheet
exorcyze is offline   Reply With Quote
Old 01-27-2009, 10:25 AM   #3 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 22
lajos is on a distinguished road
Default

You are generating the random number correctly.

However this line is bogus:
Code:
NSString *moviePath = [bundle pathForResource:@"movie"+r ofType:@"m4v"];
You could do something like:

Code:
NSString *movieName = [NSString stringWithFormat:@"movie%d.m4v",r];
NSString *moviePath = [bundle pathForResource:movieName];
__________________
codza.com
lajos is offline   Reply With Quote
Old 01-27-2009, 10:28 AM   #4 (permalink)
Mobile Geek
 
Join Date: Aug 2008
Location: Florida, USA
Posts: 365
rames44 is on a distinguished road
Send a message via AIM to rames44 Send a message via Yahoo to rames44
Default

I'm guessing you're a Java programmer?

"Adding" integers and strings doesn't work in Objective C the way it does in Java (where the integer gets converted to a string, and then the "+" means "append")

You'll need something more like:
Code:
NSString* movieName = [NSString stringWithFormat:@"movie%d", r];
NSString *moviePath = [bundle pathForResource:movieName ofType:@"m4v"];
__________________
For a little fun, check out my Biorhythms app
rames44 is offline   Reply With Quote
Old 01-27-2009, 10:37 AM   #5 (permalink)
Registered Member
 
Join Date: Jan 2009
Location: Leeds, England
Posts: 115
BenODwyer is on a distinguished road
Smile

Thanks alot, that has sorted my issue.

It is amazing how different Objective C is. I can see this is going to be a steep leaning curve!

Thanks again.
BenODwyer is offline   Reply With Quote
Old 01-27-2009, 11:13 AM   #6 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 22
lajos is on a distinguished road
Default

It's not that bad. Just read the manuals...
__________________
codza.com
lajos is offline   Reply With Quote
Old 01-27-2009, 11:52 AM   #7 (permalink)
Registered Member
 
Join Date: Sep 2008
Location: London, UK
Posts: 1,050
wuf810 is on a distinguished road
Default

Quote:
Originally Posted by BenODwyer View Post
Thanks alot, that has sorted my issue.

It is amazing how different Objective C is. I can see this is going to be a steep leaning curve!
Don't worry you'll be surprised how quickly you'll get the hang of it.
wuf810 is offline   Reply With Quote
Reply

Bookmarks

Tags
noob, random, random numbers arc4random

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: 325
6 members and 319 guests
Anwerbl, guusleijsten, HowEver, LEARN2MAKE, mottdog, Objective Zero
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,879
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 08:36 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0