2015-09-29 27 views
-1

我有一个编码问题。我试图产生一个随机数字的8位数字,例如:12345676,显示这样的数字12 34 56 76.两个数字的数字,所以一个空格,两个数字和其他空白的数字......谢谢,如何在Xcode 7和Swift 2中创建特殊格式的随机数字

我在用的代码是:

NSMutableArray *numbers; 

for(int i=0; i<3; i++){ 
float l_bound = 10;  
float h_bound = 99; 
float rndValue = (((float)arc4random()/0x100000000)*(h_bound-l_bound)+low_bound); 

int intRndValue = (int)(rndValue + 0.5); 
[numbers addObject: intRndValue] 
;} 
NSString *result = [numbers componentsJoinedByString:@" "]; 

回答

0

至少你必须初始化数组

NSMutableArray *numbers = [NSMutableArray array]; 

,你只能对象数组添加,而不是SCA如intfloat

NSString *intRndValue = [NSString stringWithFormat:@"%.0f", (rndValue + 0.5)]; 

在斯威夫特2,你可以写

var numbers = Array<String>() 
let l_bound : UInt32 = 10 
let h_bound : UInt32 = 99 

for i in 0..<3 { 
    let rndValue = Int(arc4random_uniform(h_bound - l_bound) + l_bound) 
    numbers.append("\(rndValue)") 
} 
let result = numbers.joinWithSeparator(" ")