how to extend a protocol for a delegate in objective C, then subclass an object to require a conforming delegate

The UITextView defines its delegate as @property(nonatomic, assign) id<UITextViewDelegate> delegate meaning it conforms to UITextViewDelegate, and that’s what compiler checks. If you want to use the new protocol, you need to redefine delegate to conform to your protocol: @interface MySubClass : UITextView { } @property(nonatomic, assign) id<MySubClassDelegate> delegate @end The compiler shouldn’t give any more … Read more

Recommended way to declare delegate properties with ARC

Xcode 4 Refactor > Convert to Objective-C ARC transforms: @interface XYZ : NSObject { id delegate; } @property (assign) id delegate; … @synthesize delegate; into: @interface XYZ : NSObject { id __unsafe_unretained delegate; } @property (unsafe_unretained) id delegate; … @synthesize delegate; If I remember correctly it is also mentioned in WWDC 2011 video about ARC.

Operator ‘?’ cannot be applied to operand of type ‘T’

Since not everything can be null, you have to narrow down T to be something nullable (aka an object). Structs can’t be null, and neither can enums. Adding a where on class does fix the issue: public abstract class Feature<T> where T : class So why doesn’t it just work? Invoke() yields T. If GetValue … Read more

Extension methods defined on value types cannot be used to create delegates – Why not?

In response to my other answer, Eric Smith correctly notes: “… because it would require implicitly boxing the receiver type parameter …”. Which is what happens anyway, if you do something like this: Func f = 5.ToString; Which is perfectly legal. Thinking about this has led me to a new answer. Try this on for … Read more

Implement delegates within SwiftUI Views

You need to create a view that conforms to UIViewControllerRepresentable and has a Coordinator that handles all of the delegate functionality. For example, with your example view controller and delegates: struct SomeDelegateObserver: UIViewControllerRepresentable { let vc = SomeViewController() var foo: (Data) -> Void func makeUIViewController(context: Context) -> SomeViewController { return vc } func updateUIViewController(_ uiViewController: … Read more