Swift Array Index Out of Range and Nil Values
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a JSON get request where I retrieve information about four users and then load four views with their pictures, names, etc.
I also have a function where the user can tap on those views and view more information about them.
However, I need to make sure that the array (randomFour) containing the four users contains actual information for the view they are selecting and is not nil.
I attempted to do this by counting the indexes of the array, but for some reason it isn't preventing it from going through and then crashes because the values are nil.
@objc func loadWriteView(sender: UITapGestureRecognizer) {
if sender.view == a_view || sender.view == a_pic && randomFour.count >= 1 {
setupCardWrite(user: randomFour[0])
} else if sender.view == b_view || sender.view == b_pic && randomFour.count >= 2 {
setupCardWrite(user: randomFour[1])
} else if sender.view == c_view || sender.view == c_pic && randomFour.count >= 3 {
setupCardWrite(user: randomFour[2])
} else if sender.view == d_view || sender.view == d_pic && randomFour.count >= 4 {
setupCardWrite(user: randomFour[3])
}
}
As you can see, I check that the count is greater than a certain value, but then I still sometimes receive this error:
Thread 1: Fatal error: Index out of range
And when inspecting I see:
randomFour = ([Compliments.User]) 0 values
How can I make sure that the users have been loaded properly?
ios arrays swift null indexoutofrangeexception
add a comment |
I have a JSON get request where I retrieve information about four users and then load four views with their pictures, names, etc.
I also have a function where the user can tap on those views and view more information about them.
However, I need to make sure that the array (randomFour) containing the four users contains actual information for the view they are selecting and is not nil.
I attempted to do this by counting the indexes of the array, but for some reason it isn't preventing it from going through and then crashes because the values are nil.
@objc func loadWriteView(sender: UITapGestureRecognizer) {
if sender.view == a_view || sender.view == a_pic && randomFour.count >= 1 {
setupCardWrite(user: randomFour[0])
} else if sender.view == b_view || sender.view == b_pic && randomFour.count >= 2 {
setupCardWrite(user: randomFour[1])
} else if sender.view == c_view || sender.view == c_pic && randomFour.count >= 3 {
setupCardWrite(user: randomFour[2])
} else if sender.view == d_view || sender.view == d_pic && randomFour.count >= 4 {
setupCardWrite(user: randomFour[3])
}
}
As you can see, I check that the count is greater than a certain value, but then I still sometimes receive this error:
Thread 1: Fatal error: Index out of range
And when inspecting I see:
randomFour = ([Compliments.User]) 0 values
How can I make sure that the users have been loaded properly?
ios arrays swift null indexoutofrangeexception
The answer to this post shows a solution to ensure array indexes are not out of range: stackoverflow.com/questions/25329186/…
– Norman
Nov 17 '18 at 0:55
add a comment |
I have a JSON get request where I retrieve information about four users and then load four views with their pictures, names, etc.
I also have a function where the user can tap on those views and view more information about them.
However, I need to make sure that the array (randomFour) containing the four users contains actual information for the view they are selecting and is not nil.
I attempted to do this by counting the indexes of the array, but for some reason it isn't preventing it from going through and then crashes because the values are nil.
@objc func loadWriteView(sender: UITapGestureRecognizer) {
if sender.view == a_view || sender.view == a_pic && randomFour.count >= 1 {
setupCardWrite(user: randomFour[0])
} else if sender.view == b_view || sender.view == b_pic && randomFour.count >= 2 {
setupCardWrite(user: randomFour[1])
} else if sender.view == c_view || sender.view == c_pic && randomFour.count >= 3 {
setupCardWrite(user: randomFour[2])
} else if sender.view == d_view || sender.view == d_pic && randomFour.count >= 4 {
setupCardWrite(user: randomFour[3])
}
}
As you can see, I check that the count is greater than a certain value, but then I still sometimes receive this error:
Thread 1: Fatal error: Index out of range
And when inspecting I see:
randomFour = ([Compliments.User]) 0 values
How can I make sure that the users have been loaded properly?
ios arrays swift null indexoutofrangeexception
I have a JSON get request where I retrieve information about four users and then load four views with their pictures, names, etc.
I also have a function where the user can tap on those views and view more information about them.
However, I need to make sure that the array (randomFour) containing the four users contains actual information for the view they are selecting and is not nil.
I attempted to do this by counting the indexes of the array, but for some reason it isn't preventing it from going through and then crashes because the values are nil.
@objc func loadWriteView(sender: UITapGestureRecognizer) {
if sender.view == a_view || sender.view == a_pic && randomFour.count >= 1 {
setupCardWrite(user: randomFour[0])
} else if sender.view == b_view || sender.view == b_pic && randomFour.count >= 2 {
setupCardWrite(user: randomFour[1])
} else if sender.view == c_view || sender.view == c_pic && randomFour.count >= 3 {
setupCardWrite(user: randomFour[2])
} else if sender.view == d_view || sender.view == d_pic && randomFour.count >= 4 {
setupCardWrite(user: randomFour[3])
}
}
As you can see, I check that the count is greater than a certain value, but then I still sometimes receive this error:
Thread 1: Fatal error: Index out of range
And when inspecting I see:
randomFour = ([Compliments.User]) 0 values
How can I make sure that the users have been loaded properly?
ios arrays swift null indexoutofrangeexception
ios arrays swift null indexoutofrangeexception
asked Nov 16 '18 at 21:47
Levi KLevi K
111111
111111
The answer to this post shows a solution to ensure array indexes are not out of range: stackoverflow.com/questions/25329186/…
– Norman
Nov 17 '18 at 0:55
add a comment |
The answer to this post shows a solution to ensure array indexes are not out of range: stackoverflow.com/questions/25329186/…
– Norman
Nov 17 '18 at 0:55
The answer to this post shows a solution to ensure array indexes are not out of range: stackoverflow.com/questions/25329186/…
– Norman
Nov 17 '18 at 0:55
The answer to this post shows a solution to ensure array indexes are not out of range: stackoverflow.com/questions/25329186/…
– Norman
Nov 17 '18 at 0:55
add a comment |
1 Answer
1
active
oldest
votes
Please try using brackets around the first two conditions
if (sender.view == a_view || sender.view == a_pic) && randomFour.count >= 1
Ok, thank you. I will try that. Shouldn't the brackets have no effect though?
– Levi K
Nov 16 '18 at 22:15
1
@LeviK Without the parentheses, this particular statement is treated as if you wroteif sender.view == a_view || (sender.view == a_pic && randomFour.count >= 1)
which is clearly not what you intended. Whenever you have a mix of||
and&&
in an expression, always use parentheses to avoid any ambiguity.
– rmaddy
Nov 16 '18 at 22:46
Got it, thank you so much! It usually takes a few hundred tests to get the problem, so if it ever happens again, I will let you know. Thank you so much for your time and efforts, I will mark this correct unless it ends up not working
– Levi K
Nov 16 '18 at 23:23
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f53345875%2fswift-array-index-out-of-range-and-nil-values%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
Please try using brackets around the first two conditions
if (sender.view == a_view || sender.view == a_pic) && randomFour.count >= 1
Ok, thank you. I will try that. Shouldn't the brackets have no effect though?
– Levi K
Nov 16 '18 at 22:15
1
@LeviK Without the parentheses, this particular statement is treated as if you wroteif sender.view == a_view || (sender.view == a_pic && randomFour.count >= 1)
which is clearly not what you intended. Whenever you have a mix of||
and&&
in an expression, always use parentheses to avoid any ambiguity.
– rmaddy
Nov 16 '18 at 22:46
Got it, thank you so much! It usually takes a few hundred tests to get the problem, so if it ever happens again, I will let you know. Thank you so much for your time and efforts, I will mark this correct unless it ends up not working
– Levi K
Nov 16 '18 at 23:23
add a comment |
Please try using brackets around the first two conditions
if (sender.view == a_view || sender.view == a_pic) && randomFour.count >= 1
Ok, thank you. I will try that. Shouldn't the brackets have no effect though?
– Levi K
Nov 16 '18 at 22:15
1
@LeviK Without the parentheses, this particular statement is treated as if you wroteif sender.view == a_view || (sender.view == a_pic && randomFour.count >= 1)
which is clearly not what you intended. Whenever you have a mix of||
and&&
in an expression, always use parentheses to avoid any ambiguity.
– rmaddy
Nov 16 '18 at 22:46
Got it, thank you so much! It usually takes a few hundred tests to get the problem, so if it ever happens again, I will let you know. Thank you so much for your time and efforts, I will mark this correct unless it ends up not working
– Levi K
Nov 16 '18 at 23:23
add a comment |
Please try using brackets around the first two conditions
if (sender.view == a_view || sender.view == a_pic) && randomFour.count >= 1
Please try using brackets around the first two conditions
if (sender.view == a_view || sender.view == a_pic) && randomFour.count >= 1
answered Nov 16 '18 at 21:55
Sophie S.Sophie S.
51210
51210
Ok, thank you. I will try that. Shouldn't the brackets have no effect though?
– Levi K
Nov 16 '18 at 22:15
1
@LeviK Without the parentheses, this particular statement is treated as if you wroteif sender.view == a_view || (sender.view == a_pic && randomFour.count >= 1)
which is clearly not what you intended. Whenever you have a mix of||
and&&
in an expression, always use parentheses to avoid any ambiguity.
– rmaddy
Nov 16 '18 at 22:46
Got it, thank you so much! It usually takes a few hundred tests to get the problem, so if it ever happens again, I will let you know. Thank you so much for your time and efforts, I will mark this correct unless it ends up not working
– Levi K
Nov 16 '18 at 23:23
add a comment |
Ok, thank you. I will try that. Shouldn't the brackets have no effect though?
– Levi K
Nov 16 '18 at 22:15
1
@LeviK Without the parentheses, this particular statement is treated as if you wroteif sender.view == a_view || (sender.view == a_pic && randomFour.count >= 1)
which is clearly not what you intended. Whenever you have a mix of||
and&&
in an expression, always use parentheses to avoid any ambiguity.
– rmaddy
Nov 16 '18 at 22:46
Got it, thank you so much! It usually takes a few hundred tests to get the problem, so if it ever happens again, I will let you know. Thank you so much for your time and efforts, I will mark this correct unless it ends up not working
– Levi K
Nov 16 '18 at 23:23
Ok, thank you. I will try that. Shouldn't the brackets have no effect though?
– Levi K
Nov 16 '18 at 22:15
Ok, thank you. I will try that. Shouldn't the brackets have no effect though?
– Levi K
Nov 16 '18 at 22:15
1
1
@LeviK Without the parentheses, this particular statement is treated as if you wrote
if sender.view == a_view || (sender.view == a_pic && randomFour.count >= 1)
which is clearly not what you intended. Whenever you have a mix of ||
and &&
in an expression, always use parentheses to avoid any ambiguity.– rmaddy
Nov 16 '18 at 22:46
@LeviK Without the parentheses, this particular statement is treated as if you wrote
if sender.view == a_view || (sender.view == a_pic && randomFour.count >= 1)
which is clearly not what you intended. Whenever you have a mix of ||
and &&
in an expression, always use parentheses to avoid any ambiguity.– rmaddy
Nov 16 '18 at 22:46
Got it, thank you so much! It usually takes a few hundred tests to get the problem, so if it ever happens again, I will let you know. Thank you so much for your time and efforts, I will mark this correct unless it ends up not working
– Levi K
Nov 16 '18 at 23:23
Got it, thank you so much! It usually takes a few hundred tests to get the problem, so if it ever happens again, I will let you know. Thank you so much for your time and efforts, I will mark this correct unless it ends up not working
– Levi K
Nov 16 '18 at 23:23
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.
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%2f53345875%2fswift-array-index-out-of-range-and-nil-values%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
The answer to this post shows a solution to ensure array indexes are not out of range: stackoverflow.com/questions/25329186/…
– Norman
Nov 17 '18 at 0:55