Iphone Help (String Method help Needed) Xcode

Discussion in 'iPhone/iPad Programming' started by Dylan552, Jan 22, 2011.

  1. macrumors newbie

    Joined:
    Sep 1, 2010
    #1
    Code:
    if(calledNumbers.contains(recent))
    {
        number = 1 + Bingo.nextInt(75);
    }
    so this is in java and it basically says if the random number has already been added then gernrate a new one. Also recent is the last number called placed in a string and callednumbers is a string of all the numbers. EX: "75 55 33 "So i was looking at the RangeofString in objective c and wnat to know how to do that in objective c . PLease help!

    Thanks!!
     
  2. macrumors 6502

    Joined:
    Feb 7, 2008
    #2
    I wouldn't use any type of string to store a list of numbers that I have randomly picked already. I would use an array of numbers. A simple c array of int's would probably be easiest.

    Also, rather than picking a random number, then checking if it has already been called, why not initialize the array with numbers 1-75, shuffle the array, then just call the numbers in order? Then you can eliminate the logic altogether.
     
  3. thread starter macrumors newbie

    Joined:
    Sep 1, 2010
    #3
    i was going to use an array but didnt know how to do this could you explain thx
     
  4. macrumors 6502

    Joined:
    Feb 7, 2008
    #4
    Unfortunately I don't have time to write a tutorial. Google is your friend, just search for "c array"

    Code:
    int numbers[74];
    for (int i=0;i<75;i++) {
    numbers[i]=i+1;
    }
    
    Would create an array of 75 elements, and fill it with the numbers 1 - 75.
     
  5. macrumors regular

    Joined:
    Oct 18, 2007
    Location:
    Chicago-area
    #5
    Not exactly, it would create a vector with 74 elements, fill it with numbers 1-74, and then the value 75 would be written to the memory address immediately after the end of the 74th element.

    That will likely cause weird unpredictable stuff to happen. Not in a simple program like this one, but buried somewhere in even a modestly more complex program will make you pull your hair out. (Consider that a lesson from my past; I'm very sensitive to those things now.)

    Ron C.
     
  6. dejo, Jan 23, 2011
    Last edited: Jan 23, 2011

    Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Code looks fine to me and should work as predicted. Why do you say otherwise?
     
  7. macrumors 603

    Joined:
    Aug 9, 2009
    #7
    I think you missed something.

    This declaration:
    Code:
    int numbers[74];
    
    is an array of 74 ints. The range of valid subscripts is 0 thru 73, inclusive.

    This loop:
    Code:
    for (int i=0;i<75;i++) {
    numbers[i]=i+1;
    }
    
    will overflow the array when i exceeds 73. There is one integer that exceeds 73 (the max valid array index) and is less than 75 (the loop ending condition): 74. So when i is 74, the assignment in the loop body will write to a location past the end of the array.
     
  8. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Oops, you're right. I did miss something. Thanks for the clarification! :)
     
  9. thread starter macrumors newbie

    Joined:
    Sep 1, 2010
    #9
    so how whould i chec to c if a number has already been called?
     
  10. RonC, Jan 25, 2011
    Last edited: Jan 25, 2011

    macrumors regular

    Joined:
    Oct 18, 2007
    Location:
    Chicago-area
    #10
    Try CFMutableSet

    You're on the right path. I'll assume that "calledNumbers" is some kind of container. That container has the "contains()" method defined on it.

    The equivalent iOS container is likely CFMutableSet (a subclass of CFSet). Look it up (here) and see if it works for you. You can probably get away with using int's because it stores (and likely compares) pointers (as "void *").

    I don't like that pointer comparison thing, since two objects that happen to be equal will not compare as equal when you compare pointers to these apparently equal values, but as long as you know going in it can be ok.

    if you have a CFMutableSet bob and int newRandomValue, you can use CFSetContainsValue(bob, (void *)newRandomValue) to test if the element exists in the set, and you can add it into the set by using CFSetAddValue(bob,(void*)newRandomValue).

    No guarantees on this; give it a spin and see what happens.

    Ron
     

Share This Page