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

agustinrosa

macrumors newbie
Original poster
Sep 29, 2015
3
0
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)
 
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.
 
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.

xcode-error.png


Thank you very much for your support,


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.
 
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.
 
  • Like
Reactions: agustinrosa
How to do that with some coding?

Thanks


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.
 
How to do that with some coding?

Thanks

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];
 
Last edited:
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
// <https://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)
 
Last edited:
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.
 
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.
Seems like there's a steady stream of this kind of thing going on. I don't bother following that many sites any more.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.