Swift array index out of range - arrays

Why this is not working?
var lottoTaulukko = [Int]()
var finalLottoTaulukko = [[Int]]()
var laskuri1 = 0
var laskuri2 = 0
while laskuri2 < Int(riviLaskuri) {
while laskuri1 < Int(lottoMuoto) {
showRowsLabel.stringValue += "\(lottoTaulukko[laskuri1])"
finalLottoTaulukko[laskuri2][laskuri1] = lottoTaulukko[laskuri1] // ERROR IS HERE IN THIS LINE! fatal error: Index out of range
showRowsLabel.stringValue += " "
laskuri1 += 1
}
laskuri1 = 1
showRowsLabel.stringValue += "\n"
if laskuri2==10 {showRowsLabel.stringValue += "\n"}
lottoTaulukko.shuffle()
laskuri2 += 1
}

Specify the arrays size likewise:
let size = ARRAY.count
And check out if it works for you!

Try to check if your index exists:
while laskuri1 < Int(lottoMuoto) {
if let lt = lottoTaulukko[laskuri1]?{
showRowsLabel.stringValue += "\(lottoTaulukko[laskuri1])"
if finalLottoTaulukko[laskuri2]? != nil && finalLottoTaulukko[laskuri2][laskuri1]? != nil{
finalLottoTaulukko[laskuri2][laskuri1] = lottoTaulukko[laskuri1]
}
}
showRowsLabel.stringValue += " "
laskuri1 += 1
}

Related

"; missing before instruction"

While using this code I get "; missing before instruction" error.
I believe this could have a simple explanation, but actually I can't understand what's wrong.
Also tried with switch/case statements but get same error... there could be something wrong with array...
function motori(){
var esito = [];
for (var j = 6; j < 16; j++) {
var rngRiga= foglio.getRange("AJ"+j+":BT"+j).getValues(); //store in array
var riga = rngRiga[0].join('-'); // build a string with array
Logger.log("riga = " + riga); // all right till now
if (riga.search("KOS") > -1){
var esito[j] = "KOS";} //<---- here highlights the error
else if (riga.search("OKS") > -1){
var esito[j] = "OKS";}
else if (riga.search("AN") > -1){
var esito[j] = "AN";}
else if (riga.search("OK") > -1){
var esito[j] = "OK";}
else if (riga.search("KO") > -1){
var esito[j] = "KO";}
Logger.log("result = " + esito);
Logger.log("j = " + j);
}
}
What I need is a new array with the result of the if statements.
Your issue is due to you already declared the variable esito and then inside the if you try to declare it again, so that will cause a problem because you're trying to create the same variable again. change Your code as follow:
function motori(){
var esito = [];
for (var j = 6; j < 16; j++) {
var rngRiga= foglio.getRange("AJ"+j+":BT"+j).getValues(); //store in array
var riga = rngRiga[0].join('-'); // build a string with array
Logger.log("riga = " + riga); // all right till now
if(riga.search("KOS") > -1){
esito[j] = "KOS";} //<---- here highlights the error
else if (riga.search("OKS") > -1){
esito[j] = "OKS";}
else if (riga.search("AN") > -1){
esito[j] = "AN";}
else if (riga.search("OK") > -1){
esito[j] = "OK";}
else if (riga.search("KO") > -1){
esito[j] = "KO";}
Logger.log("result = " + esito);
Logger.log("j = " + j);
}
}

Why for loop failure in angularjs?

I am having a angularjs script. In 4th if condition (if(myflag == 2)), when i am enter any minus value less than array length then that time array element removed with same product id. For Example, In my array there are 5 element of same product id, Now i want to remove 4 element then i am entering -4 then my array does not flow 4 time and splice specific index
//SCript
$scope.checkEneteredQuantity = function(qty,bagProduct){
var qty = parseInt(qty);
var nwqt = parseInt(qty);
var myflag = 0;
if(qty > 0){
myflag = 1;
}else if(qty < 0){
myflag = 2;
}
if(myflag == 1){
for(var i = 0;i < qty ; i++){
bagProduct.count = 1;
$scope.addProductInBag(bagProduct);
}
}
if(myflag == 2){
var nt = Math.abs(nwqt);
var ct = 0
for(var k = 0;k < $scope.newBagListOfProduct.length;k++){
if($scope.newBagListOfProduct[k].prod_id == bagProduct.prod_id){
if(nt != ct){
bagProduct.count = 1;
$scope.newBagListOfProduct.splice(k,1);
}
ct ++;
}
}
}
}

Changing old C for loop to new Swift loop

I have an old Xcode 7.3 Swift 2 code. I need to change these two for loops to the new swift 3 for loop syntax.
fileprivate func collapseSubItemsAtIndex(_ index : Int) {
var indexPaths = [IndexPath]()
let parent = self.findParent(index)
checkCurrentLanguage()
if lang.isEqual(to: "en")
{
//For loop 1
for (var i = index + 1; i <= index + self.engsubItems[parent].count; i += 1 ){
indexPaths.append(IndexPath(row: i, section: 0))
}
self.engtableview.deleteRows(at: indexPaths, with: UITableViewRowAnimation.fade)
self.engtotal -= self.engsubItems[parent].count
}
else{
//For loop 2
for (var i = index + 1; i <= index + self.subItems[parent].count; i += 1 ){
indexPaths.append(IndexPath(row: i, section: 0))
}
self.tableView.deleteRows(at: indexPaths, with: UITableViewRowAnimation.fade)
self.total -= self.subItems[parent].count
}
}
let count = self.engsubItems[parent].count
let rowsToDelete = ((index + 1) ... (index + count))
.map { IndexPath(row: $0, section: 0) }
self.engtableview.deleteRows(at: rowsToDelete, with: UITableViewRowAnimation.fade)
self.engtotal -= count
for i in (index+1)...(index + self.engsubItems[parent].count) {
...
}
for i in (index+1)...(index + self.subItems[parent].count) {
...
}

Get values from a Array in a Dictionary in Swift

My ambition is to have a Dictionary that contains (among other things) an array and being able to get out the values of that array.
var total: Int = 0;
let dice:[NSTextField:NSArray] = [
d4Amount:[d4OE, 4],
d6Amount:[d6OE, 6],
];
for (die, dieArray) in dice
{
let button:NSButton = dieArray[0] as! NSButton;
let num:Int = dieArray[1] as! Int;
total += DoRoll(die, oe: button, max: num);
}
In the above the line "let button:NSButton = dieArray[0]..." get's the error Thread 1: signal SIGABRT and the program fails.
First I only had the line:
total += DoRoll(die, oe: dieArray[0] as! NS Button, max: dieArray[1] as! Int);
Which didn't either work (quite obviously), but however when I do this, it works...
total += DoRoll(d4Amount, oe: d4OE, max: 4);
It works perfectly.
Any ideas??
The function DoRoll looks like this (which should not be relevant):
private func DoRoll(amount: NSTextField, oe: NSButton, max: Int) -> Int
{
let nrRolls: Int! = Int(amount.stringValue);
var total: Int = 0;
if(nrRolls != nil && nrRolls != 0)
{
OutputText.string = OutputText.string! + "Now rolling d" + String(max) + ": ";
for(var i: Int = 0; i < nrRolls; i++)
{
var randomNr: Int, specialMax: Int;
var textStr: String = "";
specialMax = max;
if(max >= 100)
{
if(max > 100)
{
specialMax = 995;
}
else if(max > 99)
{
specialMax = 95;
}
else
{
specialMax = max - 1;
}
}
repeat
{
randomNr = Int(arc4random_uniform(UInt32(max))) + 1;
total += randomNr;
if(textStr != "") { textStr = textStr + "+"; }
textStr = textStr + String(randomNr);
} while(oe.state == NSOnState && randomNr >= specialMax)
OutputText.string = OutputText.string! + textStr + " ";
}
OutputText.string = OutputText.string! + "\n";
}
return total;
}
I now know what I did wrong.
This is not at all a case of error in the code above but in the naming of items in the project. I had several different D4Amount in the same project in the View and thus that was why it crashed.
Sorry to bother you.

accessing array elements in another frame as3

I have created an Array in frame 1 and dynamically create a list of movieclips with it , and I want to create the same Array in frame 3 and those movieclips again if something is true or false.is it possible ? because when I'm trying to do this , I will get this error :
1151: A conflict exists with definition i in namespace internal.
here is my code at frame 1 :
stop();
import flash.display.MovieClip;
var p_x:uint = 90;
var p_y:uint = 108;
var my_list:String = "Games,Videos, Medias,Images,Photos,Personal Photos,Social Media,Private,Social,None,Names,Families";
var myListString:Array = my_list.split(",");
var myArray:Array=new Array ();
var listObject = 1;
for (var i:uint=0; i<12; i++)
{
var myItemList:mrb=new mrb();
myItemList.x = p_x;
myItemList.y = p_y + 80;
myItemList.y = (p_y + (listObject * 65));
myArray.push(myItemList);
myItemList.txt.text = i.toString();
myItemList.txt.text = myListString[i].toString();
myItemList.name = "item"+i;
addChild(myItemList) as MovieClip ;
listObject++;
}
and here is code at frame 3 :
var tmpCurFrame:int = currentFrame;
this.addEventListener(Event.ENTER_FRAME, handleUpdate)
function handleUpdate(e:Event):void {
if (tmpCurFrame != currentFrame) {
this.removeEventListener(Event.ENTER_FRAME, handleUpdate);
return;
}
if (so.data.fav1 != undefined)
{
if ( so.data.fav1 == "on")
{
for (var i:int = 0; i < myListString.length;)
{ if (myListString[i].indexOf() > -1)
{
var myRectangle:mrb=new mrb();
trace("found it at index: " + i);
myRectangle.x = p_x;
myRectangle.y = (p_y + (findobject * 50));
trace('p_y+80 =' + (p_y+(findobject*80)) + 'p_x = ' + p_x );
myArray.push(myRectangle);
myRectangle.txt.text = myListString[i].toString();
trace(my_array2[i].toString() );
addChild(myRectangle);
}
}
}
}
else
{
fav_1.fav_on.visible=true;
}
}
This error message simply means that you use twice the same variable i. You just have to give them differents names.

Resources