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
arrays swift
add a comment |
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
arrays swift
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
add a comment |
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
arrays swift
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
arrays swift
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
add a comment |
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
add a comment |
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) }
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
add a comment |
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) }
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
add a comment |
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) }
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
add a comment |
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) }
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) }
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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