delegates
Can I ignore delegate parameters with lambda syntax?
I believe that your first sample actually creates an anonymous function that is able to take on many different signatures whose body is the single statement Console.WriteLine…. Because it can match different signatures, it does not cause a problem. In the second sample, the lambda syntax itself defines a function that takes no parameters with … Read more
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
Why can’t I put a delegate in an interface?
You can use any of these: public delegate double CustomerDelegate(int test); public interface ITest { EventHandler<EventArgs> MyHandler{get;set;} CustomerDelegate HandlerWithCustomDelegate { get; set; } event EventHandler<EventArgs> MyEvent; }
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
Builds a Delegate from MethodInfo?
To be honest, if you don’t know the type at compile time, there isn’t a huge amount of benefit in creating a Delegate. You don’t want to use DynamicInvoke; it will be about as slow as reflection. The main exception to this is when there is a delegate-type lurking in the shadows, for example when … Read more
Active Record with Delegate and conditions
No, you can’t, but you can pass the :allow_nil => true option to return nil if the master is nil. class User < ActiveRecord::Base delegate :company, :to => :master, :allow_nil => true # … end user.master = nil user.company # => nil user.master = <#User …> user.company # => … Otherwise, you need to write … Read more