Problem with random number and styles Xcode 8, Swift 2

Discussion in 'iOS Programming' started by agustinrosa, Sep 29, 2015.

  1. agustinrosa macrumors newbie

    agustinrosa

    Joined:
    Sep 29, 2015
    #1
    I am trying to generate a Random number of 8 digits (eg: 12345676) that show the number like this (12 34 56 76). A number of two digits, so a blank space, a number of two digits and other blank space... I want to create it in Swift 2 and Xcode. THANKS,

    I am using the following code to create a random number:
    let RandomValue = arc4random_uniform(100000000)
    Numer.text=String (RandomValue)
     
  2. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #2
    Your title says 'Problem' yet you haven't stated what the problem is. Is the program crashing? Maybe post some code (using the /code tags in the menu above) then state what problem you are having.
     
  3. agustinrosa thread starter macrumors newbie

    agustinrosa

    Joined:
    Sep 29, 2015
    #3
    The problem with the previous code was that the random number that I received was XXXXXXXX, but i want to see the number as XX XX XX XX. (eg. 17626278 (now) -> 17 62 62 78 (what I want)

    I've changed a bit the code to improve it and I've got the following error in the code (screenshot) enclosed.

    [​IMG]

    Thank you very much for your support,


     
  4. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #4
    Ok, it looks like you are trying to store an int into an NSMutableArray.

    One approach would be to change the storage to one that takes ints. However, you could also just grab the 4 random numbers and add them to a string inside the for loop. This would bypass the array and save time/memory probably because you don't really need the array.

    Probably have to create an empty string outside the loop, add the random converted to a string to that string, then return the string outside the loop.
     
  5. agustinrosa thread starter macrumors newbie

    agustinrosa

    Joined:
    Sep 29, 2015
    #5
    How to do that with some coding?

    Thanks


     
  6. Dookieman, Sep 30, 2015
    Last edited: Sep 30, 2015

    Dookieman macrumors 6502

    Joined:
    Oct 12, 2009
    #6
    You can't save int type directly into an NSMutableDictionary, you need to wrap it in a type it can hold, NSNumber.


    Code:
    NSNumber *number = [[NSNumber alloc] initWithInt:<your int>];
    [numbers addObject:number];
     
  7. lloyddean, Sep 30, 2015
    Last edited: Oct 1, 2015

    lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #7
    I'm unfamiliar with swift in practice myself but it seems fairly straight forward, at least if I understand your request correctly -

    Code:
    //: Playground - noun: a place where people can play
    // <http://forums.macrumors.com/threads/problem-with-random-number-and-styles-xcode-8-swift-2.1923134>
    
    import GameplayKit
    
    let random          = GKRandomSource()
    let generator       = GKGaussianDistribution(randomSource: random, lowestValue: 10000000, highestValue: 99999999)
    let stringRandom    = String(generator.nextInt())
    
    print(stringRandom)
    
    let space           = Character(" ")
    var string          = String()
    var count           = stringRandom.characters.count
    for ch in stringRandom.characters
    {
        string.append(ch)
        if 0 != --count && 0 == (count % 2)
        {
            string.append(space)
        }
    }
    
    print(string)
    
    
     
  8. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #8
    So, the original poster has asked basically the same question over 8 separate websites and has had at least 2 useful responses, including here with code, and never acknowledged a single one.
     
  9. 1458279 Suspended

    1458279

    Joined:
    May 1, 2010
    Location:
    California
    #9
    Seems like there's a steady stream of this kind of thing going on. I don't bother following that many sites any more.
     

Share This Page