Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_TtGCs18_DictionaryStorageSSP__$ setFrame:]: unrecognized selector sent to instance 0x17ff59400'

We are facing some weird issue in the UILabel initialisation. Also it is occurring sometimes.

class TextLabel: ConfigurableView<TextLabel.Config> {
    struct Config {
        var text: String = .empty
        var font: UIFont?
        var textColor: UIColor?
        var maxLines: Int = 0
        var attributedText: NSAttributedString?
        var textAlignment: NSTextAlignment = .natural
        var truncateWithMore: Bool = false
        var onTapShowMore: (() -> Void)?
        var onTap: (() -> Void)?
        var accessibilityIdentifier: String?
    }
    
    private lazy var label: UILabel = {
        let label = UILabel() **//##### Crash is occurring in this line.** 
        label.translatesAutoresizingMaskIntoConstraints = false
        label.adjustsFontForContentSizeCategory = true
        return label
    }()
    
    private lazy var tapGesture = UITapGestureRecognizer(target: self, action: #selector(onTap))
    private var isTruncated = false
    
    override func setUp() {
        addSubview(label)
        label.equalsContainer()
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        updateContent()
    }
    
    override func setConfig(_ config: Config) {
        super.setConfig(config)
        updateContent()
    }
    
    @objc func onTap() {
        if isTruncated {
            config?.onTapShowMore?()
        } else {
            config?.onTap?()
        }
    }
    
    func updateContent() {
        guard let config = config else { return }
        label.numberOfLines = config.maxLines
        label.text = config.text
 }
}

You can find my configurable view below. 

import UIKit

class ConfigurableView<T>: UIControl {
    
    private(set) var config: T?
    
    init(_ config: T) {
        super.init(frame: .zero)
        setUp()
        setConfig(config)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUp()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUp() {
        
    }
    
    func setConfig(_ config: T) {
        self.config = config
    }
}

This crash is occurring randomly. Sometimes we could reproduce it in the app updates.

Hello ranjith_keerthi,

Please try moving your updateContent() call from layoutSubviews() to updateProperties(). As per Updating views automatically with observation tracking:

The updateProperties method runs before layout and is ideal for configuring properties like text, colors, and visibility. The layoutSubviews method handles geometry and positioning.

and the section Separate property updates from layout-

Use updateProperties() for configuring content and styling, such as putting text in labels and adjusting colors based on data. Reserve layoutSubviews() for geometry calculations, such as setting a frame for a view when using Auto Layout constraints doesn’t work for your situation. This separation improves performance by avoiding unnecessary layout passes.

If you are still seeing crashes, please attach an .ips file by following the instructions in Posting a Crash Report and Acquiring crash reports and diagnostic logs so we can analyze it accordingly.

Let me know if you'd like to know more,

Richard Yeh  Developer Technical Support

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_TtGCs18_DictionaryStorageSSP__$ setFrame:]: unrecognized selector sent to instance 0x17ff59400'
 
 
Q