Crash at tableview.reloaddata with error Unexpectedly found nil
On the click of a create button, I navigate to tableviewcontroller screen. But nothing is populated in the tableview since the array which populates the tableview hasn't been called yet.
Now once the tableview screen is reached, after a few seconds another method is called elsewhere(in another file), which in turn calls a function in this tableviewcontroller screen. This is that method of the tableviewcontroller screen which is called...
func stopIndicator(thegrpName: String) {
stopIndicator()
let realm = try! Realm()
let chatGrp = realm.objects(ChatGroup.self)
chatGroup = chatGrp
tableview.reloadData() //CRASH HAPPENS HERE
}
In this method, once I reach tableview.reloadData()
it crashes with the error Unexpectedly found nil while unwrapping an optional value..
I referred this link which seems to have a similar problem...but couldn't get much help out of it...
What could be the reason for this crash...?
EDIT 1: The numberOfRows and cellForRowAt.. is given like so...
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let chatGrp = chatGroup {
return chatGrp.count
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: ChatGroupTableViewCell = tableView.dequeueReusableCell(withIdentifier: "chatgroupIdentifier") as! ChatGroupTableViewCell
let groupChatObj = chatGroup![indexPath.row]
cell.chatLabel.text = groupChatObj.lastMessage?.text?.text
return cell
}
ios swift uitableview
|
show 8 more comments
On the click of a create button, I navigate to tableviewcontroller screen. But nothing is populated in the tableview since the array which populates the tableview hasn't been called yet.
Now once the tableview screen is reached, after a few seconds another method is called elsewhere(in another file), which in turn calls a function in this tableviewcontroller screen. This is that method of the tableviewcontroller screen which is called...
func stopIndicator(thegrpName: String) {
stopIndicator()
let realm = try! Realm()
let chatGrp = realm.objects(ChatGroup.self)
chatGroup = chatGrp
tableview.reloadData() //CRASH HAPPENS HERE
}
In this method, once I reach tableview.reloadData()
it crashes with the error Unexpectedly found nil while unwrapping an optional value..
I referred this link which seems to have a similar problem...but couldn't get much help out of it...
What could be the reason for this crash...?
EDIT 1: The numberOfRows and cellForRowAt.. is given like so...
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let chatGrp = chatGroup {
return chatGrp.count
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: ChatGroupTableViewCell = tableView.dequeueReusableCell(withIdentifier: "chatgroupIdentifier") as! ChatGroupTableViewCell
let groupChatObj = chatGroup![indexPath.row]
cell.chatLabel.text = groupChatObj.lastMessage?.text?.text
return cell
}
ios swift uitableview
Please show yournumberOfRowsInSection
andcellForRowAt
methods. Also see this for general troubleshooting and advice for this exception
– Paulw11
Nov 23 '18 at 5:39
Seemstableview
isnil
, check the outlet for connection if it's added in a storyboard.
– Satish
Nov 23 '18 at 5:43
checked that @Satish..its connected properly..
– user308123
Nov 23 '18 at 5:44
2
Set an exception breakpoint to see which line is crashing.
– Paulw11
Nov 23 '18 at 5:57
1
Instead of using exclamation marks ("!") use optional unwrapping, and print something if the value you are trying to unwrap is nil. That way, you'll know exactly which value is nil, instead of having to guess.
– Daniel Springer
Nov 23 '18 at 6:08
|
show 8 more comments
On the click of a create button, I navigate to tableviewcontroller screen. But nothing is populated in the tableview since the array which populates the tableview hasn't been called yet.
Now once the tableview screen is reached, after a few seconds another method is called elsewhere(in another file), which in turn calls a function in this tableviewcontroller screen. This is that method of the tableviewcontroller screen which is called...
func stopIndicator(thegrpName: String) {
stopIndicator()
let realm = try! Realm()
let chatGrp = realm.objects(ChatGroup.self)
chatGroup = chatGrp
tableview.reloadData() //CRASH HAPPENS HERE
}
In this method, once I reach tableview.reloadData()
it crashes with the error Unexpectedly found nil while unwrapping an optional value..
I referred this link which seems to have a similar problem...but couldn't get much help out of it...
What could be the reason for this crash...?
EDIT 1: The numberOfRows and cellForRowAt.. is given like so...
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let chatGrp = chatGroup {
return chatGrp.count
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: ChatGroupTableViewCell = tableView.dequeueReusableCell(withIdentifier: "chatgroupIdentifier") as! ChatGroupTableViewCell
let groupChatObj = chatGroup![indexPath.row]
cell.chatLabel.text = groupChatObj.lastMessage?.text?.text
return cell
}
ios swift uitableview
On the click of a create button, I navigate to tableviewcontroller screen. But nothing is populated in the tableview since the array which populates the tableview hasn't been called yet.
Now once the tableview screen is reached, after a few seconds another method is called elsewhere(in another file), which in turn calls a function in this tableviewcontroller screen. This is that method of the tableviewcontroller screen which is called...
func stopIndicator(thegrpName: String) {
stopIndicator()
let realm = try! Realm()
let chatGrp = realm.objects(ChatGroup.self)
chatGroup = chatGrp
tableview.reloadData() //CRASH HAPPENS HERE
}
In this method, once I reach tableview.reloadData()
it crashes with the error Unexpectedly found nil while unwrapping an optional value..
I referred this link which seems to have a similar problem...but couldn't get much help out of it...
What could be the reason for this crash...?
EDIT 1: The numberOfRows and cellForRowAt.. is given like so...
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let chatGrp = chatGroup {
return chatGrp.count
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: ChatGroupTableViewCell = tableView.dequeueReusableCell(withIdentifier: "chatgroupIdentifier") as! ChatGroupTableViewCell
let groupChatObj = chatGroup![indexPath.row]
cell.chatLabel.text = groupChatObj.lastMessage?.text?.text
return cell
}
ios swift uitableview
ios swift uitableview
edited Nov 23 '18 at 5:44
user308123
asked Nov 23 '18 at 5:30
user308123user308123
478
478
Please show yournumberOfRowsInSection
andcellForRowAt
methods. Also see this for general troubleshooting and advice for this exception
– Paulw11
Nov 23 '18 at 5:39
Seemstableview
isnil
, check the outlet for connection if it's added in a storyboard.
– Satish
Nov 23 '18 at 5:43
checked that @Satish..its connected properly..
– user308123
Nov 23 '18 at 5:44
2
Set an exception breakpoint to see which line is crashing.
– Paulw11
Nov 23 '18 at 5:57
1
Instead of using exclamation marks ("!") use optional unwrapping, and print something if the value you are trying to unwrap is nil. That way, you'll know exactly which value is nil, instead of having to guess.
– Daniel Springer
Nov 23 '18 at 6:08
|
show 8 more comments
Please show yournumberOfRowsInSection
andcellForRowAt
methods. Also see this for general troubleshooting and advice for this exception
– Paulw11
Nov 23 '18 at 5:39
Seemstableview
isnil
, check the outlet for connection if it's added in a storyboard.
– Satish
Nov 23 '18 at 5:43
checked that @Satish..its connected properly..
– user308123
Nov 23 '18 at 5:44
2
Set an exception breakpoint to see which line is crashing.
– Paulw11
Nov 23 '18 at 5:57
1
Instead of using exclamation marks ("!") use optional unwrapping, and print something if the value you are trying to unwrap is nil. That way, you'll know exactly which value is nil, instead of having to guess.
– Daniel Springer
Nov 23 '18 at 6:08
Please show your
numberOfRowsInSection
and cellForRowAt
methods. Also see this for general troubleshooting and advice for this exception– Paulw11
Nov 23 '18 at 5:39
Please show your
numberOfRowsInSection
and cellForRowAt
methods. Also see this for general troubleshooting and advice for this exception– Paulw11
Nov 23 '18 at 5:39
Seems
tableview
is nil
, check the outlet for connection if it's added in a storyboard.– Satish
Nov 23 '18 at 5:43
Seems
tableview
is nil
, check the outlet for connection if it's added in a storyboard.– Satish
Nov 23 '18 at 5:43
checked that @Satish..its connected properly..
– user308123
Nov 23 '18 at 5:44
checked that @Satish..its connected properly..
– user308123
Nov 23 '18 at 5:44
2
2
Set an exception breakpoint to see which line is crashing.
– Paulw11
Nov 23 '18 at 5:57
Set an exception breakpoint to see which line is crashing.
– Paulw11
Nov 23 '18 at 5:57
1
1
Instead of using exclamation marks ("!") use optional unwrapping, and print something if the value you are trying to unwrap is nil. That way, you'll know exactly which value is nil, instead of having to guess.
– Daniel Springer
Nov 23 '18 at 6:08
Instead of using exclamation marks ("!") use optional unwrapping, and print something if the value you are trying to unwrap is nil. That way, you'll know exactly which value is nil, instead of having to guess.
– Daniel Springer
Nov 23 '18 at 6:08
|
show 8 more comments
1 Answer
1
active
oldest
votes
It looks you are trying to create delegate method but in another file where you are trying to call delegate method stopIndicator
you are calling method on singleton instead which gives you an error.
So, set delegate right. First create protocol
protocol YourProtocol {
func stopIndicator(thegrpName: String)
}
then in another file create delegate property
var delegate: YourProtocol?
now when you need to call delegate method stopIndicator call this
delegate?.stopIndicator(thegrpName: ...)
now add to your ViewController protocol
ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, YourProtocol
and now somewhere set your another file class delegate as your ViewController (if its view set it in viewDidLoad if it is another ViewController set it in prepareForSegue)
fileClass.delegate = self
ok..let me try @Robert Dresler..
– user308123
Nov 23 '18 at 6:28
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%2f53441080%2fcrash-at-tableview-reloaddata-with-error-unexpectedly-found-nil%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
It looks you are trying to create delegate method but in another file where you are trying to call delegate method stopIndicator
you are calling method on singleton instead which gives you an error.
So, set delegate right. First create protocol
protocol YourProtocol {
func stopIndicator(thegrpName: String)
}
then in another file create delegate property
var delegate: YourProtocol?
now when you need to call delegate method stopIndicator call this
delegate?.stopIndicator(thegrpName: ...)
now add to your ViewController protocol
ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, YourProtocol
and now somewhere set your another file class delegate as your ViewController (if its view set it in viewDidLoad if it is another ViewController set it in prepareForSegue)
fileClass.delegate = self
ok..let me try @Robert Dresler..
– user308123
Nov 23 '18 at 6:28
add a comment |
It looks you are trying to create delegate method but in another file where you are trying to call delegate method stopIndicator
you are calling method on singleton instead which gives you an error.
So, set delegate right. First create protocol
protocol YourProtocol {
func stopIndicator(thegrpName: String)
}
then in another file create delegate property
var delegate: YourProtocol?
now when you need to call delegate method stopIndicator call this
delegate?.stopIndicator(thegrpName: ...)
now add to your ViewController protocol
ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, YourProtocol
and now somewhere set your another file class delegate as your ViewController (if its view set it in viewDidLoad if it is another ViewController set it in prepareForSegue)
fileClass.delegate = self
ok..let me try @Robert Dresler..
– user308123
Nov 23 '18 at 6:28
add a comment |
It looks you are trying to create delegate method but in another file where you are trying to call delegate method stopIndicator
you are calling method on singleton instead which gives you an error.
So, set delegate right. First create protocol
protocol YourProtocol {
func stopIndicator(thegrpName: String)
}
then in another file create delegate property
var delegate: YourProtocol?
now when you need to call delegate method stopIndicator call this
delegate?.stopIndicator(thegrpName: ...)
now add to your ViewController protocol
ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, YourProtocol
and now somewhere set your another file class delegate as your ViewController (if its view set it in viewDidLoad if it is another ViewController set it in prepareForSegue)
fileClass.delegate = self
It looks you are trying to create delegate method but in another file where you are trying to call delegate method stopIndicator
you are calling method on singleton instead which gives you an error.
So, set delegate right. First create protocol
protocol YourProtocol {
func stopIndicator(thegrpName: String)
}
then in another file create delegate property
var delegate: YourProtocol?
now when you need to call delegate method stopIndicator call this
delegate?.stopIndicator(thegrpName: ...)
now add to your ViewController protocol
ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, YourProtocol
and now somewhere set your another file class delegate as your ViewController (if its view set it in viewDidLoad if it is another ViewController set it in prepareForSegue)
fileClass.delegate = self
edited Nov 23 '18 at 7:10
answered Nov 23 '18 at 6:26
Robert DreslerRobert Dresler
6,1152626
6,1152626
ok..let me try @Robert Dresler..
– user308123
Nov 23 '18 at 6:28
add a comment |
ok..let me try @Robert Dresler..
– user308123
Nov 23 '18 at 6:28
ok..let me try @Robert Dresler..
– user308123
Nov 23 '18 at 6:28
ok..let me try @Robert Dresler..
– user308123
Nov 23 '18 at 6:28
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%2f53441080%2fcrash-at-tableview-reloaddata-with-error-unexpectedly-found-nil%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
Please show your
numberOfRowsInSection
andcellForRowAt
methods. Also see this for general troubleshooting and advice for this exception– Paulw11
Nov 23 '18 at 5:39
Seems
tableview
isnil
, check the outlet for connection if it's added in a storyboard.– Satish
Nov 23 '18 at 5:43
checked that @Satish..its connected properly..
– user308123
Nov 23 '18 at 5:44
2
Set an exception breakpoint to see which line is crashing.
– Paulw11
Nov 23 '18 at 5:57
1
Instead of using exclamation marks ("!") use optional unwrapping, and print something if the value you are trying to unwrap is nil. That way, you'll know exactly which value is nil, instead of having to guess.
– Daniel Springer
Nov 23 '18 at 6:08