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

jessejohnson147

macrumors newbie
Original poster
Jun 5, 2009
29
0
Ok here is the preemptive sorry for the stupid question. I am just starting out I have been reading everywhere for a while to get a good handle on objective C but im not the fastest learner in the world...What im trying to do is to make photo ablum that is randomized... Im NOT looking for someone to send me a sample code cause i believe that is pointless and I will never learn if someone else does everything for me...But here is my code im pretty sure it wont work... could someone tell me what is wrong with it and how to improve it...



NSInteger num = (arc4random() % 10) + 1;


if (NSInterger == 1)
{
NSImage *theImage = [NSImage imageNamed:mad:"image1.png"];
}

if (NSInterger == 2)
{
NSImage *theImage = [NSImage imageNamed:mad:"image2.png"];
}

if (NSInterger == 3)
{
NSImage *theImage = [NSImage imageNamed:mad:"image3.png"];
}

if (NSInterger == 4)
{
NSImage *theImage = [NSImage imageNamed:mad:"image4.png"];
}

if (NSInterger == 5)
{
NSImage *theImage = [NSImage imageNamed:mad:"image5.png"];
}

if (NSInterger == 6)
{
NSImage *theImage = [NSImage imageNamed:mad:"image6.png"];
}

if (NSInterger == 7)
{
NSImage *theImage = [NSImage imageNamed:mad:"image7.png"];
}

if (NSInterger == 8)
{
NSImage *theImage = [NSImage imageNamed:mad:"image8.png"];
}

if (NSInterger == 9)
{
NSImage *theImage = [NSImage imageNamed:mad:"image9.png"];
}

if (NSInterger == 10)
{
NSImage *theImage = [NSImage imageNamed:mad:"image10.png"];
}
 
I appreciate the help when you probably should of just said go back to reading... I realize that there are so many noobs out there that just what hand me outs and I am actually trying to learn this....
 
I appreciate the help when you probably should of just said go back to reading... I realize that there are so many noobs out there that just what hand me outs and I am actually trying to learn this....

No problem. Sometimes it helps to have another set of eyes look at some code.

There's probably an easier way to do what you're trying to, by using the random integer as part of the string for the image. But you're on the right track!
 
this is a huge shot in the dark...

NSInteger num = (arc4random() % 10) + 1;


NSImage *theImage = [NSImage imageNamed:mad:"image%d.png"];

Am I getting colder??
 
is this more like it:

Code:
NSString *filename = @"(arc4random() % 10) + 1";


NSString *imagePath = [NSString stringWithFormat:@"image%@.png", filename];
 
is this more like it:

Code:
NSString *filename = @"(arc4random() % 10) + 1";


NSString *imagePath = [NSString stringWithFormat:@"image%@.png", filename];
With this, filename is only going to end up containing the hard-coded string: "(arc4random() % 10) + 1" and not anything dynamic.

You probably want something more along the lines of:
Code:
NSString *imagePath = [NSString stringWithFormat:@"image%d.png", (arc4random() % 10) + 1];

But, yeah, maybe it's time to step back and revisit the basics before continuing with any real coding.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.