last day (14 days later) » 

07:20
0
Q: Swift: UItableview gives fatal error "unwrapping an Optional value"

YatendraI know this question asks many times here but I didn't get any solution from that. I created an IBoutlet of UITableView as @IBOutlet var tbl_List_new: UITableView! in some conditions, I want to hide and show this table so I am using it like this if(Condition){ tbl_List_new.hidden = t...

I already checked and also tried with created new outlel
just normal condition check true or false
@Anbu.Karthik when I used tbl_List_new,it's always show runtime error
How are you instantiating the view controller? Most likely you aren't doing so from the storyboard so the outlets aren't set
full code is jsut it, no any special code
@Paulw11 all outlets are set, I also cross checked this many times.And I am using xib
So show how you are creating the ViewController from the Xib file; if the outlets are set in the Xib but they are nil then almost certainly you have just created the VC using something like var vc=ViewController() or the crash isn't happening in this code.
@Paulw11 I have created viewcontroller from xib,and when we create Viewcontroller with xib then they connect automatically by fileowner and set the custom class name of that VC.
07:20
I know all that. Please show the line of code where you create the view controller and indicate which line of code throws the exception
Hi
Is this view controller the initial view controller in your app?
yes
import UIKit

class MonthListVC: UIViewController {

// @IBOutlet var tbl_List: UITableView!

@IBOutlet var tbl_List_new: UITableView!

@IBOutlet var lbl_noRecord: UILabel!
var arrYearMonths: Array<AnyObject> = []


let appobj = UIApplication.sharedApplication().delegate as! AppDelegate


override func viewDidLoad() {
super.viewDidLoad()
self.title = "Statics"
AddAnalytic(self.title!);
ShowMenuButton()



NSNotificationCenter.defaultCenter().addObserver(
self,
selector: #selector(loadData),
name: "AfterUpdateValues",
thsi is the my whole file code
Which line is the crash occurring on? Set an exception breakpoint.
There are lots of ! In your code. Avoid ! Every one is a potential crash. Code defensively. Don't use NSArray or [AnyObject]. Use properly typed Swift arrays
I am getting creash on tbl_List_new.hidden = true
07:35
Can you zip your whole project and upload it somewhere?
why
actually, it's not my personal project, it's my company project.
So, I can't share code
Because I can't see anything wrong with your code here (well I can see lots wrong with it but nothing that would cause the outlet to be nil) so it is probably a problem in your project file or nib file.
sorry @Paulw11
I can't share
I can share only this vc code zip
Here is a slightly more safe version of your code
import UIKit

class MonthListVC: UIViewController {

// @IBOutlet var tbl_List: UITableView!

@IBOutlet var tbl_List_new: UITableView!

let identifier = "MonthListCell"

@IBOutlet var lbl_noRecord: UILabel!
var arrYearMonths = [String]()
let appobj = UIApplication.sharedApplication().delegate as! AppDelegate


override func viewDidLoad() {
super.viewDidLoad()
tbl_List_new.registerNib(UINib(nibName: "MonthListCell", bundle: nil), forCellReuseIdentifier: identifier)
self.title = "Statics"
AddAnalytic(self.title!);
It won't address the issue with your outlet since that isn't in this code.
I would suggest that you adopt storyboard and prototype cells rather than nib files
07:56
Actually I also tried with removed all code except table outlet and table.hidden
but it's showing same error.
As I said, there is something wrong with your project file or your nib file. Unfortunately without access to those I can't help you.
Do you have to use nib files? Can you move to storyboard?
08:49
yes I have to use nib

last day (14 days later) »