Created
February 1, 2014 15:19
-
-
Save tito/8753609 to your computer and use it in GitHub Desktop.
Which delegate usage would be the best to have in Pyobjus ?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Solution 1 | |
# original solution proposed in the PR | |
# pro: easy declaration | |
# cons: instanciation is a little bit hard to write | |
# possible method conflict with multiple protocols | |
class MyObjcDelegate: | |
def connection_didFailWithError_(self, connection, error): | |
print("Protocol method got called!!", connection, error) | |
delegate = objc_delegate(MyObjcDelegate(), ['NSURLConnectionDelegate']) | |
connection = NSURLConnection.connectionWithRequest_delegate_(request, delegate) | |
# Solution 2 | |
# approach done by PyOBJC | |
# pro: close to Objective-C syntax | |
# cons: very very long method, a little verbose to instanciate | |
class MyObjcDelegate(NSObject): | |
def NSURLConnectionDelegate_connection_didFailWithError_(self, connection, error): | |
print("Protocol method got called!!", connection, error) | |
delegate = MyObjcDelegate.alloc().init() | |
connection = NSURLConnection.connectionWithRequest_delegate_(request, delegate) | |
# Solution 3 | |
# proposed by Mathieu and Gabriel, pyjnius style | |
# pro: syntax similar to PyJNIUS, short instanciation | |
# cons: possible method conflict with multiple protocols | |
class MyObjcDelegate(ObjcDelegate): | |
__protocols__ = ('NSURLConnectionDelegate', ) | |
def connection_didFailWithError_(self, connection, error): | |
print("Protocol method got called!!", connection, error) | |
delegate = MyObjcDelegate() | |
connection = NSURLConnection.connectionWithRequest_delegate_(request, delegate) | |
# Solution 4 | |
# proposed by Mathieu and Gabriel | |
# pro: easy to instanciate, good protocol separation | |
# cons: @protocol can be redundant | |
class MyObjcDelegate(ObjcDelegate): | |
@protocol('NSURLConnectionDelegate') | |
def connection_didFailWithError_(self, connection, error): | |
print("Protocol method got called!!", connection, error) | |
delegate = MyObjcDelegate() | |
connection = NSURLConnection.connectionWithRequest_delegate_(request, delegate) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment