2015-08-08 88 views
-2
var Password1 : Array = [Int]() 
var Password2 : Array = [Int]() 

while Password1.count < 800 { 

    var RandomNum1 = Int(arc4random_uniform(256)) 
    var RandomNum2 = Int(arc4random_uniform(256)) 

    Password1[Password1.count] = RandomNum1 
    Password2[Password2.count] = RandomNum2 

} 

Password1[Password1.count] = RandomNum1線這似乎 - > EXC_BAD_INSTRUCTION(代碼= EXC_I386_INVOP,子碼=爲0x0)陣列索引超出範圍

+0

http://stackoverflow.com/questions/24002733/add-an-element-to-an-array – m69

回答

2

Password1Password2陣列是空的。因此,您不能在沒有錯誤的情況下編入索引。使用append來創建數組:

Password1.append(RandomNum1) 
Password2.append(RandomNum2) 

此外,變量名應該以小寫字母開頭。


如果你希望能夠索引到陣列,用零首先初始化它們:

var password1 = [Int](count: 800, repeatedValue: 0) 
var password2 = [Int](count: 800, repeatedValue: 0) 

for i in 0..<800 { 
    var randomNum1 = Int(arc4random_uniform(256)) 
    var randomNum2 = Int(arc4random_uniform(256)) 

    password1[i] = randomNum1 
    password2[i] = randomNum2 
} 
0

您試圖在不存在的鍵取代的價值。這是PHP方式,而不是Swift。

由於您已經初始化了數組,因此您需要對它們進行調用追加以在數組末尾添加元素。