Fatal error: Index Out of range? BUT IS IT?











up vote
-2
down vote

favorite












I have a very weird array and I am not sure what I am missing, something very simple I am sure. Here is my code, This code loops a struct with arrays of objects and variables and attempts to find the objects which category = 1, when this is true, it sends the array of objects into another array. Everything works fine, until I try to copy the array into the sorted array, more comments in code. I am trying to sort a tableView when the user clicks a certain category, the 4 possible categories are 1,30,31,32 (don't ask why)



var courses = [Course]()
var sortedCourses = [Course]()
@objc func catOthersButtonObj(_ sender: UIButton){ //<- needs `@objc`
var count = 0
let courseCount = courses.count
let otherCat = 1

// set count to loop the number of detected arrays
// this category looks for the number 1

print("courseCount = " + String(courseCount)) // to debug if courseCount is accurate, it is
//let noOfCat = courses.categories.count

while (count < courseCount)
{

print("count = " + String(count)) // to debug if count is increasing every loop, it is
let totalNoCat = (courses[count].categories?.count)!
var catCount = 0

// while entering a loop of arrays of objects, it searches for the array "categories" and sets totalNoCat = number of categories, this is neccesary because one object can have many categories
// catCount is used to loop through the number of detect categories

if (totalNoCat == 0)
{
break
}

// If no categories is detected, next array

while (catCount < totalNoCat)
{
print("totalNoCat = " + String(totalNoCat))
print("catCount = " + String(catCount))

// debug, check if totalNoCat is detected
// debug, catCount if number of categories is detected and added
if (courses[count].categories?[catCount] == otherCat)
{
print("category Detected = " + String((courses[count].categories?[catCount])!))
// see if category is 1, when it is 1, this is triggered, so its working fine
let currentNoSortedCourses = sortedCourses.count
print("currentNoSortedCourses = " + String(currentNoSortedCourses))
// check the number of sorted array, this is to add array into the next array
// if there is 0 number of sorted arrays, then sorted[0] should be course[detectarray category = 0 array]
//print(courses[count]) checks if courses[count] has data, it does
sortedCourses[currentNoSortedCourses] = courses[count] //where the error is
//here it is sortedCourses[0] = courses[7] ( 7 is which the array detected the correct category
break
}
catCount = catCount + 1
print("Category Detected = " + String((courses[count].categories?[catCount - 1])!))

//debug, if category not 1, show what it is

}
count = count + 1
}

}




Print results

courseCount = 50
count = 0
totalNoCat = 2
catCount = 0
Category Detected = 31
totalNoCat = 2
catCount = 1
Category Detected = 30
count = 1
totalNoCat = 2
catCount = 0
Category Detected = 31
totalNoCat = 2
catCount = 1
Category Detected = 30
count = 2
totalNoCat = 1
catCount = 0
Category Detected = 30
count = 3
totalNoCat = 2
catCount = 0
Category Detected = 32
totalNoCat = 2
catCount = 1
Category Detected = 30
count = 4
totalNoCat = 2
catCount = 0
Category Detected = 32
totalNoCat = 2
catCount = 1
Category Detected = 30
count = 5
totalNoCat = 1
catCount = 0
Category Detected = 32
count = 6
totalNoCat = 2
catCount = 0
Category Detected = 32
totalNoCat = 2
catCount = 1
Category Detected = 30
count = 7
totalNoCat = 1
catCount = 0
category Detected = 1
currentNoSortedCourses = 0









share|improve this question




















  • 1




    What is your actual question?
    – user1118321
    Nov 15 at 6:20










  • What is the question? Yes, if you get out of bounds, it is out of bounds.
    – Desdenova
    Nov 15 at 6:23















up vote
-2
down vote

favorite












I have a very weird array and I am not sure what I am missing, something very simple I am sure. Here is my code, This code loops a struct with arrays of objects and variables and attempts to find the objects which category = 1, when this is true, it sends the array of objects into another array. Everything works fine, until I try to copy the array into the sorted array, more comments in code. I am trying to sort a tableView when the user clicks a certain category, the 4 possible categories are 1,30,31,32 (don't ask why)



var courses = [Course]()
var sortedCourses = [Course]()
@objc func catOthersButtonObj(_ sender: UIButton){ //<- needs `@objc`
var count = 0
let courseCount = courses.count
let otherCat = 1

// set count to loop the number of detected arrays
// this category looks for the number 1

print("courseCount = " + String(courseCount)) // to debug if courseCount is accurate, it is
//let noOfCat = courses.categories.count

while (count < courseCount)
{

print("count = " + String(count)) // to debug if count is increasing every loop, it is
let totalNoCat = (courses[count].categories?.count)!
var catCount = 0

// while entering a loop of arrays of objects, it searches for the array "categories" and sets totalNoCat = number of categories, this is neccesary because one object can have many categories
// catCount is used to loop through the number of detect categories

if (totalNoCat == 0)
{
break
}

// If no categories is detected, next array

while (catCount < totalNoCat)
{
print("totalNoCat = " + String(totalNoCat))
print("catCount = " + String(catCount))

// debug, check if totalNoCat is detected
// debug, catCount if number of categories is detected and added
if (courses[count].categories?[catCount] == otherCat)
{
print("category Detected = " + String((courses[count].categories?[catCount])!))
// see if category is 1, when it is 1, this is triggered, so its working fine
let currentNoSortedCourses = sortedCourses.count
print("currentNoSortedCourses = " + String(currentNoSortedCourses))
// check the number of sorted array, this is to add array into the next array
// if there is 0 number of sorted arrays, then sorted[0] should be course[detectarray category = 0 array]
//print(courses[count]) checks if courses[count] has data, it does
sortedCourses[currentNoSortedCourses] = courses[count] //where the error is
//here it is sortedCourses[0] = courses[7] ( 7 is which the array detected the correct category
break
}
catCount = catCount + 1
print("Category Detected = " + String((courses[count].categories?[catCount - 1])!))

//debug, if category not 1, show what it is

}
count = count + 1
}

}




Print results

courseCount = 50
count = 0
totalNoCat = 2
catCount = 0
Category Detected = 31
totalNoCat = 2
catCount = 1
Category Detected = 30
count = 1
totalNoCat = 2
catCount = 0
Category Detected = 31
totalNoCat = 2
catCount = 1
Category Detected = 30
count = 2
totalNoCat = 1
catCount = 0
Category Detected = 30
count = 3
totalNoCat = 2
catCount = 0
Category Detected = 32
totalNoCat = 2
catCount = 1
Category Detected = 30
count = 4
totalNoCat = 2
catCount = 0
Category Detected = 32
totalNoCat = 2
catCount = 1
Category Detected = 30
count = 5
totalNoCat = 1
catCount = 0
Category Detected = 32
count = 6
totalNoCat = 2
catCount = 0
Category Detected = 32
totalNoCat = 2
catCount = 1
Category Detected = 30
count = 7
totalNoCat = 1
catCount = 0
category Detected = 1
currentNoSortedCourses = 0









share|improve this question




















  • 1




    What is your actual question?
    – user1118321
    Nov 15 at 6:20










  • What is the question? Yes, if you get out of bounds, it is out of bounds.
    – Desdenova
    Nov 15 at 6:23













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I have a very weird array and I am not sure what I am missing, something very simple I am sure. Here is my code, This code loops a struct with arrays of objects and variables and attempts to find the objects which category = 1, when this is true, it sends the array of objects into another array. Everything works fine, until I try to copy the array into the sorted array, more comments in code. I am trying to sort a tableView when the user clicks a certain category, the 4 possible categories are 1,30,31,32 (don't ask why)



var courses = [Course]()
var sortedCourses = [Course]()
@objc func catOthersButtonObj(_ sender: UIButton){ //<- needs `@objc`
var count = 0
let courseCount = courses.count
let otherCat = 1

// set count to loop the number of detected arrays
// this category looks for the number 1

print("courseCount = " + String(courseCount)) // to debug if courseCount is accurate, it is
//let noOfCat = courses.categories.count

while (count < courseCount)
{

print("count = " + String(count)) // to debug if count is increasing every loop, it is
let totalNoCat = (courses[count].categories?.count)!
var catCount = 0

// while entering a loop of arrays of objects, it searches for the array "categories" and sets totalNoCat = number of categories, this is neccesary because one object can have many categories
// catCount is used to loop through the number of detect categories

if (totalNoCat == 0)
{
break
}

// If no categories is detected, next array

while (catCount < totalNoCat)
{
print("totalNoCat = " + String(totalNoCat))
print("catCount = " + String(catCount))

// debug, check if totalNoCat is detected
// debug, catCount if number of categories is detected and added
if (courses[count].categories?[catCount] == otherCat)
{
print("category Detected = " + String((courses[count].categories?[catCount])!))
// see if category is 1, when it is 1, this is triggered, so its working fine
let currentNoSortedCourses = sortedCourses.count
print("currentNoSortedCourses = " + String(currentNoSortedCourses))
// check the number of sorted array, this is to add array into the next array
// if there is 0 number of sorted arrays, then sorted[0] should be course[detectarray category = 0 array]
//print(courses[count]) checks if courses[count] has data, it does
sortedCourses[currentNoSortedCourses] = courses[count] //where the error is
//here it is sortedCourses[0] = courses[7] ( 7 is which the array detected the correct category
break
}
catCount = catCount + 1
print("Category Detected = " + String((courses[count].categories?[catCount - 1])!))

//debug, if category not 1, show what it is

}
count = count + 1
}

}




Print results

courseCount = 50
count = 0
totalNoCat = 2
catCount = 0
Category Detected = 31
totalNoCat = 2
catCount = 1
Category Detected = 30
count = 1
totalNoCat = 2
catCount = 0
Category Detected = 31
totalNoCat = 2
catCount = 1
Category Detected = 30
count = 2
totalNoCat = 1
catCount = 0
Category Detected = 30
count = 3
totalNoCat = 2
catCount = 0
Category Detected = 32
totalNoCat = 2
catCount = 1
Category Detected = 30
count = 4
totalNoCat = 2
catCount = 0
Category Detected = 32
totalNoCat = 2
catCount = 1
Category Detected = 30
count = 5
totalNoCat = 1
catCount = 0
Category Detected = 32
count = 6
totalNoCat = 2
catCount = 0
Category Detected = 32
totalNoCat = 2
catCount = 1
Category Detected = 30
count = 7
totalNoCat = 1
catCount = 0
category Detected = 1
currentNoSortedCourses = 0









share|improve this question















I have a very weird array and I am not sure what I am missing, something very simple I am sure. Here is my code, This code loops a struct with arrays of objects and variables and attempts to find the objects which category = 1, when this is true, it sends the array of objects into another array. Everything works fine, until I try to copy the array into the sorted array, more comments in code. I am trying to sort a tableView when the user clicks a certain category, the 4 possible categories are 1,30,31,32 (don't ask why)



var courses = [Course]()
var sortedCourses = [Course]()
@objc func catOthersButtonObj(_ sender: UIButton){ //<- needs `@objc`
var count = 0
let courseCount = courses.count
let otherCat = 1

// set count to loop the number of detected arrays
// this category looks for the number 1

print("courseCount = " + String(courseCount)) // to debug if courseCount is accurate, it is
//let noOfCat = courses.categories.count

while (count < courseCount)
{

print("count = " + String(count)) // to debug if count is increasing every loop, it is
let totalNoCat = (courses[count].categories?.count)!
var catCount = 0

// while entering a loop of arrays of objects, it searches for the array "categories" and sets totalNoCat = number of categories, this is neccesary because one object can have many categories
// catCount is used to loop through the number of detect categories

if (totalNoCat == 0)
{
break
}

// If no categories is detected, next array

while (catCount < totalNoCat)
{
print("totalNoCat = " + String(totalNoCat))
print("catCount = " + String(catCount))

// debug, check if totalNoCat is detected
// debug, catCount if number of categories is detected and added
if (courses[count].categories?[catCount] == otherCat)
{
print("category Detected = " + String((courses[count].categories?[catCount])!))
// see if category is 1, when it is 1, this is triggered, so its working fine
let currentNoSortedCourses = sortedCourses.count
print("currentNoSortedCourses = " + String(currentNoSortedCourses))
// check the number of sorted array, this is to add array into the next array
// if there is 0 number of sorted arrays, then sorted[0] should be course[detectarray category = 0 array]
//print(courses[count]) checks if courses[count] has data, it does
sortedCourses[currentNoSortedCourses] = courses[count] //where the error is
//here it is sortedCourses[0] = courses[7] ( 7 is which the array detected the correct category
break
}
catCount = catCount + 1
print("Category Detected = " + String((courses[count].categories?[catCount - 1])!))

//debug, if category not 1, show what it is

}
count = count + 1
}

}




Print results

courseCount = 50
count = 0
totalNoCat = 2
catCount = 0
Category Detected = 31
totalNoCat = 2
catCount = 1
Category Detected = 30
count = 1
totalNoCat = 2
catCount = 0
Category Detected = 31
totalNoCat = 2
catCount = 1
Category Detected = 30
count = 2
totalNoCat = 1
catCount = 0
Category Detected = 30
count = 3
totalNoCat = 2
catCount = 0
Category Detected = 32
totalNoCat = 2
catCount = 1
Category Detected = 30
count = 4
totalNoCat = 2
catCount = 0
Category Detected = 32
totalNoCat = 2
catCount = 1
Category Detected = 30
count = 5
totalNoCat = 1
catCount = 0
Category Detected = 32
count = 6
totalNoCat = 2
catCount = 0
Category Detected = 32
totalNoCat = 2
catCount = 1
Category Detected = 30
count = 7
totalNoCat = 1
catCount = 0
category Detected = 1
currentNoSortedCourses = 0






arrays swift






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 at 5:54









rmaddy

237k27308374




237k27308374










asked Nov 15 at 5:54









Zack Cheang Weng Seong

308




308








  • 1




    What is your actual question?
    – user1118321
    Nov 15 at 6:20










  • What is the question? Yes, if you get out of bounds, it is out of bounds.
    – Desdenova
    Nov 15 at 6:23














  • 1




    What is your actual question?
    – user1118321
    Nov 15 at 6:20










  • What is the question? Yes, if you get out of bounds, it is out of bounds.
    – Desdenova
    Nov 15 at 6:23








1




1




What is your actual question?
– user1118321
Nov 15 at 6:20




What is your actual question?
– user1118321
Nov 15 at 6:20












What is the question? Yes, if you get out of bounds, it is out of bounds.
– Desdenova
Nov 15 at 6:23




What is the question? Yes, if you get out of bounds, it is out of bounds.
– Desdenova
Nov 15 at 6:23












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










Without going much into the structure of the code and how it can be written in a more "Swifty" way, I can see the problem is that this is not a good way to append to an array in Swift.



            let currentNoSortedCourses = sortedCourses.count
sortedCourses[currentNoSortedCourses] = courses[count]


This is accessing an index that is out of bounds and causes a crash. To append to an array in Swift use append method.



        sortedCourses.append(courses[count])


Whilst I'm here I might as well tell you that a better way (one liner) to solve your problem probably be something like this



    sortedCourses = courses.filter { $0.categories.contains(1) }





share|improve this answer





















  • your one liner works like magic, Thanks SOOO MUCH, you make my codes look ancient!
    – Zack Cheang Weng Seong
    Nov 15 at 7:29










  • Oh man, only if i knew i didn't need to go through that logic to code all that ahead of time
    – Zack Cheang Weng Seong
    Nov 15 at 7:29











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313234%2ffatal-error-index-out-of-range-but-is-it%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










Without going much into the structure of the code and how it can be written in a more "Swifty" way, I can see the problem is that this is not a good way to append to an array in Swift.



            let currentNoSortedCourses = sortedCourses.count
sortedCourses[currentNoSortedCourses] = courses[count]


This is accessing an index that is out of bounds and causes a crash. To append to an array in Swift use append method.



        sortedCourses.append(courses[count])


Whilst I'm here I might as well tell you that a better way (one liner) to solve your problem probably be something like this



    sortedCourses = courses.filter { $0.categories.contains(1) }





share|improve this answer





















  • your one liner works like magic, Thanks SOOO MUCH, you make my codes look ancient!
    – Zack Cheang Weng Seong
    Nov 15 at 7:29










  • Oh man, only if i knew i didn't need to go through that logic to code all that ahead of time
    – Zack Cheang Weng Seong
    Nov 15 at 7:29















up vote
2
down vote



accepted










Without going much into the structure of the code and how it can be written in a more "Swifty" way, I can see the problem is that this is not a good way to append to an array in Swift.



            let currentNoSortedCourses = sortedCourses.count
sortedCourses[currentNoSortedCourses] = courses[count]


This is accessing an index that is out of bounds and causes a crash. To append to an array in Swift use append method.



        sortedCourses.append(courses[count])


Whilst I'm here I might as well tell you that a better way (one liner) to solve your problem probably be something like this



    sortedCourses = courses.filter { $0.categories.contains(1) }





share|improve this answer





















  • your one liner works like magic, Thanks SOOO MUCH, you make my codes look ancient!
    – Zack Cheang Weng Seong
    Nov 15 at 7:29










  • Oh man, only if i knew i didn't need to go through that logic to code all that ahead of time
    – Zack Cheang Weng Seong
    Nov 15 at 7:29













up vote
2
down vote



accepted







up vote
2
down vote



accepted






Without going much into the structure of the code and how it can be written in a more "Swifty" way, I can see the problem is that this is not a good way to append to an array in Swift.



            let currentNoSortedCourses = sortedCourses.count
sortedCourses[currentNoSortedCourses] = courses[count]


This is accessing an index that is out of bounds and causes a crash. To append to an array in Swift use append method.



        sortedCourses.append(courses[count])


Whilst I'm here I might as well tell you that a better way (one liner) to solve your problem probably be something like this



    sortedCourses = courses.filter { $0.categories.contains(1) }





share|improve this answer












Without going much into the structure of the code and how it can be written in a more "Swifty" way, I can see the problem is that this is not a good way to append to an array in Swift.



            let currentNoSortedCourses = sortedCourses.count
sortedCourses[currentNoSortedCourses] = courses[count]


This is accessing an index that is out of bounds and causes a crash. To append to an array in Swift use append method.



        sortedCourses.append(courses[count])


Whilst I'm here I might as well tell you that a better way (one liner) to solve your problem probably be something like this



    sortedCourses = courses.filter { $0.categories.contains(1) }






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 at 6:21









muvaaa

31738




31738












  • your one liner works like magic, Thanks SOOO MUCH, you make my codes look ancient!
    – Zack Cheang Weng Seong
    Nov 15 at 7:29










  • Oh man, only if i knew i didn't need to go through that logic to code all that ahead of time
    – Zack Cheang Weng Seong
    Nov 15 at 7:29


















  • your one liner works like magic, Thanks SOOO MUCH, you make my codes look ancient!
    – Zack Cheang Weng Seong
    Nov 15 at 7:29










  • Oh man, only if i knew i didn't need to go through that logic to code all that ahead of time
    – Zack Cheang Weng Seong
    Nov 15 at 7:29
















your one liner works like magic, Thanks SOOO MUCH, you make my codes look ancient!
– Zack Cheang Weng Seong
Nov 15 at 7:29




your one liner works like magic, Thanks SOOO MUCH, you make my codes look ancient!
– Zack Cheang Weng Seong
Nov 15 at 7:29












Oh man, only if i knew i didn't need to go through that logic to code all that ahead of time
– Zack Cheang Weng Seong
Nov 15 at 7:29




Oh man, only if i knew i didn't need to go through that logic to code all that ahead of time
– Zack Cheang Weng Seong
Nov 15 at 7:29


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313234%2ffatal-error-index-out-of-range-but-is-it%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?