How to properly implement lazy instantiation with an #IBOutlet property in Swift - ios

I am Learning iOS Development with Big Nerd Ranch's latest iOS book. I have chosen to implement their apps in Swift. In one of their apps, they have the following code in Objective C:
- (UIView *)headerView
{
// If you have not loaded the header view yet...
if (!_headerView) {
// Load HeaderView.xib
[[NSBundle mainBundle] loadNibNamed:#"HeaderView" owner:self options:nil]
}
return _headerView;
}
Apple's Swift guide on "#IBOutlet":
When you declare an outlet in Swift, the compiler automatically converts the type to a weak implicitly unwrapped optional and assigns it an initial value of nil. In effect, the compiler replaces #IBOutlet var name: Type with #IBOutlet weak var name: Type! = nil.
As it was pointed out in Lazy loading Properties in swift, there are a couple of different options. None of them in that post explicitly mention lazy initialization with #IBOutlet, so I've done by best to implement their suggestions, and would like to know what would be considered best practices.
Attempt #1(failed): following a similar pattern, as the example from AppDelegate.swift. This brings the issue "'IBOutlet' attribute requires property to be mutable"
#IBOutlet var headerView : UIView {
// If the HeaderView has not been loaded yet...
if !_headerView {
// Load HeaderView.xib
NSBundle.mainBundle().loadNibNamed("HeaderView", owner: self, options: nil)
}
return _headerView!
}
var _headerView : UIView? = nil
Attempt #2(failed): using any variation of "#lazy" with "#IBOutlet" didn't worked because "#lazy" needs an initializer, but if a closure is used, then "#IBOutlet" has the same issue as from Attempt #1
Attempt #3(successful?): this is the only way I was able to get this to work. I got the idea from a somewhat different question, Lazy property initialization in Swift. My understanding of what is happening is headerView is actually declared as "#IBOutlet weak var headerView : UIView! = nil", will only be initialized once with the TableViewController subclass I have, and that initialization will be "lazy" in that it only occurs when the TableViewController needs to be loaded.
#IBOutlet var headerView : UIView
func loadHeaderView() {
// If the HeaderView has not been loaded yet...
if !headerView {
// Load HeaderView.xib
println("loaded HeaderView")
NSBundle.mainBundle().loadNibNamed("HeaderView", owner: self, options: nil)
}
}
override func viewDidLoad() {
super.viewDidLoad()
loadHeaderView()
tableView.tableHeaderView = headerView
}
So, how can this be improved?
Is viewDidLoad() the correct function to use?
Thanks

A lazily loaded outlet makes no sense- if it's an outlet, it's populated when loading the nib, not from code. If you're loading it from code, it doesn't need to be an outlet, so you can use #lazy.

You aren't actually providing a closure for headerView with that code, you're declaring it as a read-only computed property. #IBOutlet properties need to be mutable so the XIB/Storyboard can do its magic, so you'd need to implement it with both a getter and a setter, like this:
#IBOutlet var headerView : UIView {
get {
if !_headerView {
NSBundle.mainBundle().loadNibNamed("HeaderView", owner: self, options: nil)
}
return _headerView!
}
set {
_headerView = newValue
}
}
var _headerView : UIView? = nil

The following works...
#IBOutlet lazy var headerView : UIView? = {
return NSBundle.mainBundle().loadNibNamed("HeaderView", owner: self, options: nil)[0] as? UIView
}()
then set the headerView
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableHeaderView = headerView
}

You can also use a didSet:
#IBOutlet weak var profileImageView: UIImageView! {
didSet {
profileImageView.image = profileImage
profileImageView.layer.masksToBounds = true
profileImageView.layer.cornerRadius = 16
}
}

Related

There is difference in behaviour when an instance variable is computed instead of being stored

I have a custom UIView class that is initiated from the xib file. It has instance property called title of type String?. Whenever, the title property is set, the text of a UITextField gets changed to the value of the title property.
If the title property is a stored property, the program works as expected.
If the title property is a computed property, then the program crashes with EXC_BAD_ACCESS error which I assume is because an IBOutlet had not yet been initialized.
Can anyone explain why if title is a stored property, it works but if it is an computed property it fails?
Following is the source code-
The NibView is a subclass of UIView and handles the loading of xib file
class NibView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
loadNib()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
loadNib()
}
}
The implementation of loadNib method is inside an extension
extension UIView {
func loadNib() {
guard let view = nib.instantiate(withOwner: self, options: nil).first as? UIView else { return }
view.frame = bounds
addSubview(view)
}
}
The definition of nib property on UIView is in another extension
extension UIView {
static var nib: UINib {
return UINib(nibName: String(describing: self), bundle: nil)
}
var nib: UINib {
return type(of: self).nib
}
}
The following class is the class which has the title property.
class ProgressView: NibView {
var title: String? {
didSet {
titleLabel.text = title
}
}
#IBOutlet private weak var titleLabel: UILabel!
}
The above class is used as follows-
let view = ProgressView()
addSubview(view)
view.title = "Loading"
Running the above code works as expected.
However if the implementation of ProgressView is changed to use a computed property as below, then it fails
class ProgressView: NibView {
var title: String? {
get {
return titleLabel.text
}
set {
titleLabel.text = newValue
}
}
#IBOutlet private weak var titleLabel: UILabel!
}
Can anyone point out why where is difference in behaviour when the title property is computed instead of being stored?
Edit -
The main thread crashes with "Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)"
The method on top of the call stack is "ProgressView.title.modify".
Edit 2-
I am not sure what I have done but I am unable to reproduce the issue after restarting xcode. Even if computed property is used, it works as expected.
Your description is far from explanatory, but I'm guessing that there is a ProgressView nib in which the File's Owner is a ProgressView, and there is an titleLabel outlet from the File's owner to a label inside the nib. (I assume this because otherwise I can't explain your use of withOwner: self.)
On that assumption I can't reproduce any problem. Both your ways of expressing title work just fine for me. I put print statements to make sure the right one was being called, and it is; no matter whether this is a didSet or the setter of a computed property, we load just fine and I see the "Loading" text.
My code is in a view controller's viewDidLoad, if that makes a difference.
(By the way, I regard your use of ProgressView() with suspicion. This results in a zero-size view. It might not seem to make any difference, but it's a bad idea. The label is a subview of the zero-size view. If the zero-size view clipped its subviews, the label would be invisible. Even if the zero-size view does not clip its subviews, if the label were a button, the button would not work. Zero-size views are a bad idea. You should give your ProgressView a real frame.)

UIImageView outlet nil in custom UIView with xib

I've created a custom UIView with multiple IBOutlets including a UIImageView, and three UILabels. Despite setting the image value in the awakeFromNib() function, I'm still getting a nil value for the outlet when attempting to set it. The UIView was constructed using a custom xib file.
I put the set operation in awakeFromNib() so as to assure the outlets had been initialized prior to setting them, yet this failed to help. I'm not sure if it's an issue with the xib file as I've only ever used custom xibs when making a custom table cell, so perhaps the issue is rooted there?
class VotingCard: UIView {
#IBOutlet weak var proPicImg: UIImageView!
#IBOutlet weak var nameLabel: UILabel!
#IBOutlet weak var hometownLabel: UILabel!
#IBOutlet weak var ratingLabel: UILabel!
var proPic = UIImage()
var name = ""
var hometown = ""
var rating = 0.0
init(pic: UIImage, rush: Rush) {
proPic = pic
name = rush.fullName
hometown = rush.homeTown
rating = rush.compositeRating
super.init(frame: CGRect(x: 0, y: 0, width: 250, height: 300))
}
override func awakeFromNib() {
proPicImg.image = proPic
nameLabel.text = name
hometownLabel.text = hometown
ratingLabel.text = "\(rating)"
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
I should view a simple card with the profile image and respective data visible, yet instead it crashes.
Your problem is either
1-The imageView isn't connected to Ib , and the fix is to connect it
Or
2- You create an object of the view with init(pic: UIImage, rush: Rush) { and that for sure won't load the UI with view , hence all outlets are nil , and the fix to add this method
class func getInstance(_ pic: UIImage, rush: Rush) -> VotingCard {
let v = Bundle.main.loadNibNamed("VotingCard", owner: self, options: nil)!.first as! VotingCard
v.proPic = pic
v.name = rush.fullName
v.hometown = rush.homeTown
v.rating = rush.compositeRating
return v
}
Call
let v = VotingCard.getInstance(<#image#>,rush:<#rush#>)
v.frame = // set some frame
// ready for use
EDIT
NIB-based development is sort-of deprecated:
For iOS developers, using storyboards is the recommended way to design
user interfaces.
https://developer.apple.com/library/archive/documentation/General/Conceptual/DevPedia-CocoaCore/NibFile.html
But, looks like you're missing this step:
At runtime, you load a nib file using the method loadNibNamed:owner:
or a variant thereof. The File’s Owner is a placeholder in the nib
file for the object that you pass as the owner parameter of that
method. Whatever connections you establish to and from the File’s
Owner in the nib file in Interface Builder are reestablished when you
load the file at runtime.
See as an example:
https://medium.com/#umairhassanbaig/ios-swift-creating-a-custom-view-with-xib-ace878cd41c5
If you want to do it the recommended way with Storyboard (apparently not, but this is for general audience):
https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/WorkWithViewControllers.html
viewDidLoad()—Called when the view controller’s content view (the top
of its view hierarchy) is created and loaded from a storyboard. The
view controller’s outlets are guaranteed to have valid values by the
time this method is called. Use this method to perform any additional
setup required by your view controller.
So with the UIViewController method, put your code in viewDidLoad() instead.
Keep in mind this is always called, so this will overwrite the work of your custom init() function. Perhaps this is what you were running into.
I would refactor it to avoid ugly code. Create the Rush object always (declare as Optional as stored property), but initialize it in awakeFromNib() for the XIB case, and copy the input object in the custom-init case.
Then viewDidLoad() has no conditionals.

weak reference in ios9 and ios8

[Problem soluted!Just want to know why there is such a difference in ios8 and ios9] I was making a register view controller these days and face with some problem about weak reference.
and below is some part of the code(swift)
problem come when I use an iphone6 ios8.1
it crashed. And then I noticed that the weak reference is not proper here. But the code runs well in my ios9 iphone6s. I ran this code on an iphone6 ios8 simulator, app crashed. So I think there is some thing different in processing weak reference in ios8 and ios9, But who can explain why..?
class VC: UIViewController {
weak var verifyTextField: UITextField?
override func viewdidload() {
//....
verifyTextField = newTextField();
view.addSubview(verifyTextField!);
}
func newTextField() -> UITextField {
let ntf = UITextField();
//do some settings to ntf;
return ntf;
}
}
You set your new UITextField instance to the weak var verifyTextField but before you add it as a subview (which increments the retain count) it is deallocated (the count is 0 since the var is weak) so verifyTextField! crashes, the crash you're getting is most likely the famous
Unexpectedly found nil while unwrapping an Optional
It's easy to fix it
Don't use a weak var
Don't force unwrap (use if let instead)
The code should be as follows:
class VC: UIViewController {
var verifyTextField: UITextField? //should not be weak
override func viewdidload() {
//....
verifyTextField = newTextField()
if let verifyTextField = verifyTextField {
view.addSubview(verifyTextField!)
}
}
func newTextField() -> UITextField {
let ntf = UITextField()
//do some settings to ntf
return ntf
}
}
Looks like your object is deallocated instantly after initialization because you don't store any strong reference for it.
Try this code:
override func viewdidload() {
//....
let verifyTextField = newTextField();
view.addSubview(verifyTextField);
self.verifyTextField = verifyTextField;
}
Also no need to use weak reference here, because verifyTextField doesn't have reference to your VC, so you won't get a retain cycle.

FirebaseUi - Swift - Cast value of UITableViewCell to custom class

I have an issue when working with a custom class for the UITableViewCell with Firebase-UI. Here is my code:
In my TableViewController:
self.dataSource = FirebaseTableViewDataSource(ref: firebaseRef, cellClass: MyEventTableViewCell.self, cellReuseIdentifier: "MyEventTableViewCell", view: self.tableView)
self.dataSource.populateCellWithBlock { (cell, obj) -> Void in
let snap = obj as! FDataSnapshot
print(cell)
let myEventCell = cell as! MyEventTableViewCell
myEventCell.eventNameLabel.text = "hello"
}
In my MyEventTableViewCell:
class MyEventTableViewCell: UITableViewCell {
#IBOutlet weak var eventImageView: UIImageView!
#IBOutlet weak var eventNameLabel: UILabel!
var error:String?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
I got:
'fatal error: unexpectedly found nil while unwrapping an Optional
value"
on this line:
myEventcell.eventNameLabel.text = "hello"
Weird is that the "print" gives the following output:
<test.MyEventTableViewCell: 0x7dab0400; baseClass = UITableViewCell; frame = (0 0; 320 44); autoresize = W; layer = <CALayer: 0x7be738c0">>
What do we have to do to manage subclass of UITableViewCell?
PS: I am working with the storyboard to define my Custom cell and I am working with Xcode 7.
FirebaseUI developer here:
If you're using storyboards/prototype cells, use the constructor that has the prototypeReuseIdentifier vs the cellReuseIdentifier (see here). This is an unfortunate wart caused by how iOS started doing UICollectionViews but left the UITableView implementation of Storyboards different. TL;DR: Storyboards automatically register the cell reuse identifier for you, and if you try to register it again it'll override it and consider it different, meaning you don't see anything. See the FirebaseUI docs on Using Storyboards and Prototype Cells for more info (though looks like I need to add in the prototype bit, so apologies for the confusion).
It seems you forgot to implement init in your custom cell
For FirebaseUI-ios github
Create a custom subclass of UITableViewCell or UICollectionViewCell, with or without the XIB file. Make sure to instantiate -initWithStyle: reuseIdentifier: to instantiate a UITableViewCell or -initWithFrame: to instantiate a UICollectionViewCell. You can then hook the custom class up to the implementation of FirebaseTableViewDataSource.

Custom UIView subclass with XIB in Swift

I'm using Swift and Xcode 6.4 for what it's worth.
So I have a view controller that will be containing some multiple pairs of UILabels and UIImageViews. I wanted to put the UILabel-UIImageView pair into a custom UIView, so I could simply reuse the same structure repeatedly within the aforementioned view controller. (I'm aware this could be translated into a UITableView, but for the sake of ~learning~ please bear with me). This is turning out to be a more convoluted process than I imagined it would be, I'm having trouble figuring out the "right" way to make this all work in IB.
Currently I've been floundering around with a UIView subclass and corresponding XIB, overriding init(frame:) and init(coder), loading the view from the nib and adding it as a subview. This is what I've seen/read around the internet so far. (This is approximately it: http://iphonedev.tv/blog/2014/12/15/create-an-ibdesignable-uiview-subclass-with-code-from-an-xib-file-in-xcode-6).
This gave me the problem of causing an infinite loop between init(coder) and loading the nib from the bundle. Strangely none of these articles or previous answers on stack overflow mention this!
Ok so I put a check in init(coder) to see if the subview had already been added. That "solved" that, seemingly. However I started running into an issue with my custom view outlets being nil by the time I try to assign values to them.
I made a didSet and added a breakpoint to take a look...they are definitely being set at one point, but by the time I try to, say, modify the textColor of a label, that label is nil.
I'm kind of tearing my hair out here.
Reusable components seem like software design 101, but I feel like Apple is conspiring against me. Should I be looking to use container VCs here? Should I just be nesting views and having a stupidly huge amount of outlets in my main VC? Why is this so convoluted? Why do everyone's examples NOT work for me?
Desired result (pretend the whole thing is the VC, the boxes are the custom uiviews I want):
Thanks for reading.
Following is my custom UIView subclass. In my main storyboard, I have UIViews with the subclass set as their class.
class StageCardView: UIView {
#IBOutlet weak private var stageLabel: UILabel! {
didSet {
NSLog("I will murder you %#", stageLabel)
}
}
#IBOutlet weak private var stageImage: UIImageView!
var stageName : String? {
didSet {
self.stageLabel.text = stageName
}
}
var imageName : String? {
didSet {
self.stageImage.image = UIImage(named: imageName!)
}
}
var textColor : UIColor? {
didSet {
self.stageLabel.textColor = textColor
}
}
var splatColor : UIColor? {
didSet {
let splatImage = UIImage(named: "backsplat")?.tintedImageWithColor(splatColor!)
self.backgroundColor = UIColor(patternImage: splatImage!)
}
}
// MARK: init
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
if self.subviews.count == 0 {
setup()
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
func setup() {
if let view = NSBundle.mainBundle().loadNibNamed("StageCardView", owner: self, options: nil).first as? StageCardView {
view.frame = bounds
view.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
addSubview(view)
}
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/
}
EDIT: Here's what I've been able to get so far...
XIB:
Result:
Problem: When trying to access label or image outlets, they are nil. When checking at breakpoint of said access, the label and image subviews are there and the view hierarchy is as expected.
I'm OK with doing this all in code if thats what it takes, but I'm not huge into doing Autolayout in code so I'd rather not if there's a way to avoid it!
EDIT/QUESTION SHIFT:
I figured out how to make the outlets stop being nil.
Inspiration from this SO answer: Loaded nib but the view outlet was not set - new to InterfaceBuilder except instead of assigning the view outlet I assigned the individual component outlets.
Now this was at the point where I was just flinging shit at a wall and seeing if it'd stick. Does anyone know why I had to do this? What sort of dark magic is this?
General advice on view re-use
You're right, re-usable and composable elements is software 101. Interface Builder is not very good at it.
Specifically, xibs and storyboard are great ways to define views by re-using views that are defined in code. But they are not very good for defining views that you yourself wish to re-use within xibs and storyboards. (It can be done, but it is an advanced exercise.)
So, here's a rule of thumb. If you are defining a view that you want to re-use from code, then define it however you wish. But if you are defining a view that you want to be able to re-use possibly from within a storyboard, then define that view in code.
So in your case, if you're trying to define a custom view which you want to re-use from a storyboard, I'd do it in code. If you are dead set on defining your view via a xib, then I'd define a view in code and in its initializer have it initialize your xib-defined view and configure that as a subview.
Advice in this case
Here's roughly how you'd define your view in code:
class StageCardView: UIView {
var stageLabel = UILabel(frame:CGRectZero)
var stageImage = UIImageView(frame:CGRectZero)
override init(frame:CGRect) {
super.init(frame:frame)
setup()
}
required init(coder aDecoder:NSCoder) {
super.init(coder:aDecoder)
setup()
}
private func setup() {
stageImage.image = UIImage(named:"backsplat")
self.addSubview(stageLabel)
self.addSubview(stageImage)
// configure the initial layout of your subviews here.
}
}
You can now instantiate this in code and or via a storyboard, although you won't get a live preview in Interface Builder as is.
And alternatively, here's roughly how you might define a re-usable view based fundamentally on a xib, by embedding the xib-defined view in a code-defined view:
class StageCardView: UIView {
var embeddedView:EmbeddedView!
override init(frame:CGRect) {
super.init(frame:frame)
setup()
}
required init(coder aDecoder:NSCoder) {
super.init(coder:aDecoder)
setup()
}
private func setup() {
self.embeddedView = NSBundle.mainBundle().loadNibNamed("EmbeddedView",owner:self,options:nil).lastObject as! UIView
self.addSubview(self.embeddedView)
self.embeddedView.frame = self.bounds
self.embeddedView.autoresizingMask = .FlexibleHeight | .FlexibleWidth
}
}
Now you can use the code-defined view from storyboards or from code, and it will load its nib-defined subview (and there's still no live preview in IB).
I was able to work it around but the solution is a little bit tricky. It's up to debate if the gain is worth an effort but here is how I implemented it purely in interface builder
First I defined a custom UIView subclass named P2View
#IBDesignable class P2View: UIView
{
#IBOutlet private weak var titleLabel: UILabel!
#IBOutlet private weak var iconView: UIImageView!
#IBInspectable var title: String? {
didSet {
if titleLabel != nil {
titleLabel.text = title
}
}
}
#IBInspectable var image: UIImage? {
didSet {
if iconView != nil {
iconView.image = image
}
}
}
override init(frame: CGRect)
{
super.init(frame: frame)
awakeFromNib()
}
required init?(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)
}
override func awakeFromNib()
{
super.awakeFromNib()
let bundle = Bundle(for: type(of: self))
guard let view = bundle.loadNibNamed("P2View", owner: self, options: nil)?.first as? UIView else {
return
}
view.translatesAutoresizingMaskIntoConstraints = false
addSubview(view)
let bindings = ["view": view]
let verticalConstraints = NSLayoutConstraint.constraints(withVisualFormat:"V:|-0-[view]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: bindings)
let horizontalConstraints = NSLayoutConstraint.constraints(withVisualFormat:"H:|-0-[view]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: bindings)
addConstraints(verticalConstraints)
addConstraints(horizontalConstraints)
}
titleLabel.text = title
iconView.image = image
}
This is how it looks like in interface builder
This is how I embedded this custom view in the example view controller defined on a storyboard. Properties of P2View are set in the attributes inspector.
There are 3 points worth mentioning
First:
Use the Bundle(for: type(of: self)) when loading the nib. This is because the interface builder renders the designables in the separate process which main bundle is not the same as your main bundle.
Second:
#IBInspectable var title: String? {
didSet {
if titleLabel != nil {
titleLabel.text = title
}
}
}
When combining IBInspectables with IBOutlets you have to remember that the didSet functions are called before awakeFromNib method. Because of that, the outlets are not initialized and your app will probably crash at this point. Unfortunatelly you cannot omit the didSet function because the interface builder won't render your custom view so we have to leave this dirty if here.
Third:
titleLabel.text = title
iconView.image = image
We have to somehow initialize our controls. We were not able to do it when didSet function was called so we have to use the value stored in the IBInspectable properties and initialize them at the end of the awakeFromNib method.
This is how you can implement a custom view on a Xib, embed it on a storyboard, configure it on a storyboard, have it rendered and have a non-crashing app. It requires a hack, but it's possible.

Resources