Nil optional unwrap error when firebase value gets set in UICollectionViewCell - swift

I call firebase database to retrieve names of locations. I store the data in an array of dictionaries, then populate collectionView cells with the data from the array. when the collection view cell is selected, I segue to another view controller and pass the data to a collectionViewCell inside the view controller.
The console prints the correct values, but when I set a UILabel text equal to the value, the compiler throw a nil optional unwrap error.
Passing the data to the view controller in didSelectItemAt:
Setting a value to a UILabel text:
Console prints the value:
Am I passing the data incorrectly?
Im not sure why the console is printing the value, but compiler throws an error. Any insight as to why this happens, and how to correct this is very appreciated.
EDIT: ozzieozumo was correct, I was incorrectly instantiating my cell class. I was creating a new instance of my cell, but had no connection to storyboard, which led to my label being nil.
SOLUTION: I edited my segue method to take my dictionary as a parameter. instead of instantiating the CollectionViewCell in the method, I instantiated the CollectionViewController and passed my dictionary to the CollectionViewController. In the Controller under cellForItemAt, I set pass the data to the cell.

The assignment statement fails because the left hand side is nil, not the right hand side.
Your outlet is nil because of the way you are instantiating the cell instance.
let detailImageCell = LocationDetailImagesCell()
That will create a new instance of your cell but that instance has nothing to do with your storyboards/xibs, and so the outlets will be uninitialized.
That is your immediate problem here.

The label is not linked in the Interface Builder, it is that element that is nil

Related

Though I did optional binding, I got error “fatal error: unexpectedly found nil while unwrapping an Optional value"

I received data from my application server, and then assign that data to ios's object(articles).
And using that object(articles), I save values to cell's label in tableView.
After that, when user click the cell of tableView, this app move to another ViewController(NoticeArticleVC). Before moving, I assign value of object(articles) to that ViewController's variable like below code.
However, as usual, I did optional binding using 'if let' statement. But I got error as if I didn't do optional binding.
Though I cleaned build folder, and then rebuilded, it didn't work.
Please let me know what's wrong with my code.
You've instantiated the new ViewController, but I don't think titleLabel will be set until the view has loaded. You will need to present the new view controller and then set the label. Or better, set a variable on the new ViewController and only set the label field from within that VC code (e.g. in/after viewDidLoad).

How to set nil value for reuseidentifier for uitableview cell while dequeueReusableCellWithIdentifier?

Hi I am loading nib file for uitableview cell in swift.
I am registering cell but for that want to use nil ReusableCellWithIdentifier.
Because I don't want to reuse cell.
In objective -C we can pass nil value in ReusableCellWithIdentifier to stop reusing cell but how to do in Swift .
If I am using init for custom cell then other outlets are coming nil and getting fatal Error.
PROBLEM IS IF I AM USING DEQUEREUSEABLE WITH IDENTIFIER LIKE "CUSTOMECELL" THEN IF I CHAGE ANY CELL PROPERTY THEN IT IS REFLECTING IN OTHER CELL ALSO DUE TO REUSE IDENTIFIER.
Actually problem was while reusing identifier it was not maintaining state for other cell and I also want to reuse the cell because I am loading from nib.
So for each cell I need to maintain state in Array or dictionary.

Unexpectedly found nil for optional value - which is not nil after optional chaining

I've an array of images in CoreData, which is fetched and used in the Master View Controller and passed to the Detailed View Controller and again to a Container View Controller. The images are successfully shown in the Master and Detail controllers, but not the Container View Controller.
Within the Container View Controller, I am using conditional binding to unwrap the image in cellForItemAtIndexPath.
In the debugger, print("did actually: (images.count)") on load shows I've an image in the array, and imageForCell is not nil - it is a UIImage - BUT, I still get unexpectedly found nil while unwrapping an Optional value...
So to give the answer to this question, first always check what is nil. You assumed the value you unwrapped was nil, but your view was actually nil.
A tip: use the gaurd statement. Inside the guard statement you have to specify what your code has to do in an error situation. After the guard statement, you can use the values that you unwrapped. Your current code just shows an empty cell without any messages when your image is nil. If it would not show an image, you would not know what went wrong.
Personally I like to check every possible optional in my methods. This makes methods annoyingly large, but prevents errors. I also like to specify the type of the object, but that is entirely up to you. For me it's mostly the difference between "UIImage" and "UIImage?". In your situation the UIImage might still be nil.
To continue to your next question, why it still wasn't working. So I looked around at how a UICollectionView would initialize its cells when loaded from the storyboard. People said that when manually using registerClass in those scenario's it would overwrite the view loaded from the storyboard. See: How to set a UILabel in UICollectionViewCell. I asked you to remove it, to confirm my suspicions.
I'm assuming you only need to use registerClass when not loading a view from a storyboard or nib/xib.

Can't update Label from other class in Swift

I have a little problem with my code so that I can't call a function. I created a function func changeLabelText(text: String) { mylabel.stringValue = text } in the ViewController with that I can update the Text in the Label. When I try to call the function from another class in the Project, I get a runtime error(EXC_BAD_Instr...) and the Debugger holds on the line where I try to change the Label's text, with the error: fatal error: unexpectedly found nil while unwrapping an Optional value. Where is the problem? Can someone help me please!
Regarding the "unexpectedly found nil while unwrapping an Optional value" error, the problem is that you have an optional (judging from the code snippet you've provided, it must be an implicitly unwrapped optional) that is nil. Most likely, mylabel is nil. Either print the value or add a breakpoint and examine the property in the debugger to confirm.
If it is nil, you then have to figure out why. In our discussion, it turns out that you're trying to call the Log event in the view controller:
ViewController().Log("asdf")
The problem is that this doesn't call Log in the existing view controller, but rather the ViewController() expression ends up instantiating a new, completely unrelated view controller with its outlets not hooked up to anything. Thus the attempt to update the implicitly unwrapped outlet will cause the error you shared with us.
If you want this separate class (a database manager object) to inform the view controller of event in order to allow the view controller to update the UI, there are three common approaches:
Completion/progress handlers that are closures/blocks.
This is used for simple interface where the database needs to inform view controller when the request is done.
Delegate-protocol pattern.
The delegation pattern (usually conforming to some well-established protocol) is used for rich interfaces where the database needs to inform the view controller of a variety of different types of events.
Notification pattern.
Notifications are used when you want a loosely coupled interface between the database object and whatever is handling these notifications. The view controller might register itself as an observer of any notifications of a particular name with the defaultCenter() of the NSNotificationCenter. The database object can then post notifications of that name (supplying details via the userInfo dictionary) and the view controller will be informed of these events.
A few things are you calling an instance of the view controller properly
e.g if the class name is ViewController, in the the view controller that you are calling the function make sure you use
let VC = ViewController ()
and then use VC.yourFunctionName
also instead of stringValue use .text

swift: send value back to UITableViewController from dynamic cell with UIDatePicker

I've got a dynamic table with a cell for a UIDatePicker. There's a function in the UITableViewCell class that updates a variable when the picker changes. I'm just having trouble getting that value back to the UITableViewController class. I can print the correct variable while spinning the picker, but when I go to reloadRowsAtIndexPaths for the tableView I need the value available.
Since there's no segue, I can't get a protocol/delegate to work. I imagine a global variable might also work, but that's cheating. Any suggestions?
You can use NSUserDefaults for storing values globally which can access anywhere into your project. you can set values this way for your picker.
NSUserDefaults.standardUserDefaults().setObject("YouValueForm", forKey: "YourKey")
After that you can access this value anywhere in your project this way:
let yourVar = NSUserDefaults.standardUserDefaults().objectForKey("YourKey") as! String
You can modify this as per your need. and once you got your values you can do reloadRowsAtIndexPaths.
Hope this will help.