PDA

View Full Version : image viewer




jessejohnson147
Jun 7, 2009, 09:36 PM
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:@"image1.png"];
}

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

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

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

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

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

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

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

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

if (NSInterger == 10)
{
NSImage *theImage = [NSImage imageNamed:@"image10.png"];
}



MacToddB
Jun 7, 2009, 09:42 PM
Could it be that you mix "NSInteger" and "NSInterger"?

jessejohnson147
Jun 7, 2009, 09:55 PM
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....

MacToddB
Jun 7, 2009, 09:58 PM
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!

jessejohnson147
Jun 7, 2009, 10:10 PM
this is a huge shot in the dark...

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


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

Am I getting colder??

robbieduncan
Jun 8, 2009, 03:35 AM
this is a huge shot in the dark...

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


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

That will try and load an image called image%d.png. It won't try and replace the %d with the NSInteger as you've not told it to. You need to use NSString stringWithFormat: (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/clm/NSString/stringWithFormat:) or similar.

jessejohnson147
Jun 8, 2009, 11:01 AM
is this more like it:

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


NSString *imagePath = [NSString stringWithFormat:@"image%@.png", filename];

dejo
Jun 8, 2009, 11:38 AM
is this more like it:

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:
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.