image viewer

Discussion in 'iOS Programming' started by jessejohnson147, Jun 7, 2009.

  1. jessejohnson147 macrumors newbie

    Joined:
    Jun 5, 2009
    #1
    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"];
    }
     
  2. MacToddB macrumors 6502a

    MacToddB

    Joined:
    Aug 21, 2007
    Location:
    Rochester, NY
    #2
    Could it be that you mix "NSInteger" and "NSInterger"?
     
  3. jessejohnson147 thread starter macrumors newbie

    Joined:
    Jun 5, 2009
    #3
    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....
     
  4. MacToddB macrumors 6502a

    MacToddB

    Joined:
    Aug 21, 2007
    Location:
    Rochester, NY
    #4
    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!
     
  5. jessejohnson147 thread starter macrumors newbie

    Joined:
    Jun 5, 2009
    #5
    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??
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    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: or similar.
     
  7. jessejohnson147 thread starter macrumors newbie

    Joined:
    Jun 5, 2009
    #7
    is this more like it:

    Code:
    NSString *filename = @"(arc4random() % 10) + 1";
    
    
    NSString *imagePath = [NSString stringWithFormat:@"image%@.png", filename];
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    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.
     

Share This Page