Hey all,
so I'm working on a program that will randomly generate some information to be bulk loaded into a database for school. The first function I'm trying to get working is generating a random phone number.
Here is my thought process for this:
have a for loop run through 12 iterations (0-11)
if the current iteration is 3 or 7 insert a -
else find a random number between 0 and 9, convert the number to a string, append that string onto the phone number string.
return the phone number string
I wrote the code and had it show me the returned phone number in a alert box. My problem is that it is returning phone numbers such as 111-111-1111. I put in a breakpoint to see why it was returning numbers like that and found that it was actually finding random numbers and appending them properly, then returning the wrong string made up of whatever the last digit found was.
Here is the code to generate the phone number:
Does anyone have any idea why it would be returning the wrong string?
Thanks,
Dan
so I'm working on a program that will randomly generate some information to be bulk loaded into a database for school. The first function I'm trying to get working is generating a random phone number.
Here is my thought process for this:
have a for loop run through 12 iterations (0-11)
if the current iteration is 3 or 7 insert a -
else find a random number between 0 and 9, convert the number to a string, append that string onto the phone number string.
return the phone number string
I wrote the code and had it show me the returned phone number in a alert box. My problem is that it is returning phone numbers such as 111-111-1111. I put in a breakpoint to see why it was returning numbers like that and found that it was actually finding random numbers and appending them properly, then returning the wrong string made up of whatever the last digit found was.
Here is the code to generate the phone number:
Code:
- (NSString *)randomPhone {
NSString *phone = [[NSString alloc] initWithString:@""];
int digit;
NSString *stringDigit = [[NSString alloc] init];
for (int i = 0; i < 12; i++) {
if (i == 3 || i == 7) {
phone = [phone stringByAppendingString:@"-"];
} else {
digit = [self randomNumberBetween:0 and:9];
stringDigit = [NSString stringWithFormat:@"%d", digit];
phone = [phone stringByAppendingString:stringDigit];
}
}
return phone;
}
Thanks,
Dan
Last edited by a moderator: