Post

Replies

Boosts

Views

Activity

NSUnknownKeyException when connecting property IB
I am trying to integrate a UIKit view into my SwiftUI project using UIViewRepresentable as an intermediate. A TestViewVC.swift file was created containing just a simple IBOutlet: import Foundation import UIKit class TestViewVC: UIViewController { @IBOutlet weak var testLabel: UILabel! } A corresponding TestViewVC.xib file was creating containing just a single label centred on screen. The TestViewVC was then assigned as the File Owner's class. These are then displayed within the SwiftUI context through the use of a TestRepresentableView.swift and TestViewVC.swift class (shown below). //TestRepresentableView.swift import SwiftUI struct TestRepresentableView: UIViewRepresentable { func makeUIView(context: Context) -> UIView { // Load the XIB file and return its view let nib = UINib(nibName: "TestViewVC", bundle: nil) return nib.instantiate(withOwner: nil, options: nil)[0] as! UIView } func updateUIView(_ uiView: UIView, context: Context) { // Update the view here if needed } } //TestViewVC.swift import SwiftUI @main struct TestApp: App { var body: some Scene { WindowGroup { TestRepresentableView() } } } The app runs fine on debug however an NSUnknownKeyException is raised whenever label is connected to the IBOutlet. (Or any other outlet for that matter, even simply connecting the view results in the same error). Any idea of which this is happening?
1
0
564
May ’23
objc_getAssociatedObject and Runtime attributes
I have the following extension which was used to automatically save/retrieve runtime attributes unique to a UIImageView: import UIKit var imgAttributeKey:String? = nil extension UIImageView { var imgAttribute: String? { get { return objc_getAssociatedObject(self, &imgAttributeKey) as? String } set { objc_setAssociatedObject(self, &imgAttributeKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN) } } } This was working fine but after trying the code again recently, the getters were always returning nil. Did something change in Swift 5 version that could be breaking this implementation? Any suggestions on how to go about it?
2
0
1.2k
Jun ’21