Main questionI am setting a font for a UITextView that is a subview in a custom view. I only set it if the user didn't specify the font size in the Interface Builder.I have the following working codeif self.view.font == nil || self.view.font!.fontName != mongolFontName {        view.font = UIFont(name: mongolFontName, size: defaultFontSize)}but the if line seems more verbose than it needs to be. Is there a more concise way of getting the same result?Extra background infoThis is a continuation in the same vane of a previous question. After making changes based on advice from the answers there (though still more to do), the code I have now is import UIKit@IBDesignable class UIMongolTextView: UIView {    // ********* Unique to TextView *********    private let view = UITextView()    let mongolFontName = ChimeeWhiteMirrored    let defaultFontSize: CGFloat = 17    @IBInspectable var text: String {        get {            return view.text        }        set {            view.text = newValue        }    }    @IBInspectable var fontSize: CGFloat {        get {            if let font = view.font {                return font.pointSize            } else {                return 0.0            }        }        set {            view.font = UIFont(name: mongolFontName, size: newValue)        }    }    func setup() {        view.backgroundColor = UIColor.clearColor()        // set font if user didn't specify size in IB        if self.view.font == nil || self.view.font!.fontName != mongolFontName {            view.font = UIFont(name: mongolFontName, size: defaultFontSize)        }    }    // *******************************************    // ****** General code for Mongol views ******    // *******************************************    private var oldWidth: CGFloat = 0    private var oldHeight: CGFloat = 0    // This method gets called if you create the view in the Interface Builder    required init(coder aDecoder: NSCoder) {        super.init(coder: aDecoder)    }    // This method gets called if you create the view in code    override init(frame: CGRect){        super.init(frame: frame)        self.setup()    }    override func awakeFromNib() {        super.awakeFromNib()        self.setup()    }    override func layoutSubviews() {        super.layoutSubviews()        // layoutSubviews gets called multiple times, only need it once        if self.frame.height == oldHeight && self.frame.width == oldWidth {            return        } else {            oldWidth = self.frame.width            oldHeight = self.frame.height        }        // Remove the old rotation view        if self.subviews.count > 0 {            self.subviews[0].removeFromSuperview()        }        // setup rotationView container        let rotationView = UIView()        rotationView.frame = CGRect(origin: CGPointZero, size: CGSize(width: self.bounds.height, height: self.bounds.width))        self.addSubview(rotationView)        // transform rotationView (so that it covers the same frame as self)        rotationView.transform = translateRotateFlip()        // add view        view.frame = rotationView.bounds        rotationView.addSubview(view)    }    func translateRotateFlip() -> CGAffineTransform {        var transform = CGAffineTransformIdentity        // translate to new center        transform = CGAffineTransformTranslate(transform, (self.bounds.width / 2)-(self.bounds.height / 2), (self.bounds.height / 2)-(self.bounds.width / 2))        // rotate counterclockwise around center        transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2))        // flip vertically        transform = CGAffineTransformScale(transform, -1, 1)        return transform    }}
Set a font for a UITextView if the font is nil or not equal to desired font
ios;swift;null;user interface;uikit
There's a much easier way of doing this.if self.view.font?.fontName != mongolFontName {    view.font = UIFont(name: mongolFontName, size: defaultFontSize)}We should always prefer optional binding and optional unwrapping (?) to forced (!).When the value we're optionally unwrapping is nil, we just get nil back.  And of course, nil and mongolFontName aren't going to be the same (unless you've set up mongolFontName as nil), so we enter the if's body.Consider this example from the playground: