objective-c-protocol

Category for a class that conforms to a protocol [duplicate]

こ雲淡風輕ζ 提交于 2019-12-25 11:54:06
问题 This question already has answers here : Can a category implement a protocol in Objective C? (1 answer) Making class conform to protocol with category for existing methods (2 answers) Closed 6 years ago . I'm trying to implement a category for a UIViewController and I want to be certain that the object conforms to a certain protocol. Something like this: #import <UIKit/UIKit.h> @interface UIViewController<MyProtocol> (Category) @end Is this possible? 回答1: Swap category and protocol:

Category for a class that conforms to a protocol [duplicate]

醉酒当歌 提交于 2019-12-25 11:54:05
问题 This question already has answers here : Can a category implement a protocol in Objective C? (1 answer) Making class conform to protocol with category for existing methods (2 answers) Closed 6 years ago . I'm trying to implement a category for a UIViewController and I want to be certain that the object conforms to a certain protocol. Something like this: #import <UIKit/UIKit.h> @interface UIViewController<MyProtocol> (Category) @end Is this possible? 回答1: Swap category and protocol:

Is it possible to define a property with Class type that conforms to protocol?

戏子无情 提交于 2019-12-12 15:08:31
问题 For example, I have MyFancyData protocol. How can I specify that MyFancyDataClass property accepts only classes that conforms to this protocol. @interface MyObject : NSObject @property Class MyFancyDataClass; 回答1: @property id<MyFancyData> myFancyDataClass; 回答2: Do you mean something like this? @interface MyObject : NSObject @property (nonatomic, assign) Class<MyFancyData> cls; @end 来源: https://stackoverflow.com/questions/27760662/is-it-possible-to-define-a-property-with-class-type-that

Creating a category for classes that implement a specific protocol in Objective-C?

被刻印的时光 ゝ 提交于 2019-11-29 00:11:31
Short problem description Can I extend UIView with a category, but have it only work on subclasses that implement a specific protocol ( WritableView )? I.e. can I do something like the following? @interface UIView<WritableView> (foo) // SYNTAX ERROR - (void)setTextToDomainOfUrl:(NSString *)text; - (void)setTextToIntegerValue:(NSInteger)value; - (void)setCapitalizedText:(NSString *)text; @end @implementation UIView<WritableView> (foo) // implementation of 3 above methods would go here @end Detailed problem description Imagine I want the following category function added to any instance of

Protocol versus Category

时光总嘲笑我的痴心妄想 提交于 2019-11-28 15:39:57
Can anyone explain the differences between Protocols and Categories in Objective-C? When do you use one over the other? mipadi A protocol is the same thing as an interface in Java: it's essentially a contract that says, "Any class that implements this protocol will also implement these methods." A category, on the other hand, just binds methods to a class. For example, in Cocoa , I can create a category for NSObject that will allow me to add methods to the NSObject class (and, of course, all subclasses), even though I don't really have access to NSObject . To summarize: a protocol specifies

Creating a category for classes that implement a specific protocol in Objective-C?

夙愿已清 提交于 2019-11-27 15:12:58
问题 Short problem description Can I extend UIView with a category, but have it only work on subclasses that implement a specific protocol ( WritableView )? I.e. can I do something like the following? @interface UIView<WritableView> (foo) // SYNTAX ERROR - (void)setTextToDomainOfUrl:(NSString *)text; - (void)setTextToIntegerValue:(NSInteger)value; - (void)setCapitalizedText:(NSString *)text; @end @implementation UIView<WritableView> (foo) // implementation of 3 above methods would go here @end

Protocol versus Category

倾然丶 夕夏残阳落幕 提交于 2019-11-27 09:20:25
问题 Can anyone explain the differences between Protocols and Categories in Objective-C? When do you use one over the other? 回答1: A protocol is the same thing as an interface in Java: it's essentially a contract that says, "Any class that implements this protocol will also implement these methods." A category, on the other hand, just binds methods to a class. For example, in Cocoa , I can create a category for NSObject that will allow me to add methods to the NSObject class (and, of course, all

How to create class methods that conform to a protocol shared between Swift and Objective-C?

断了今生、忘了曾经 提交于 2019-11-26 23:04:53
I've been learning Swift lately. I decided to write a hybrid Swift/Objective-C app that did compute-intensive tasks using the same algorithm implemented in both languages. The program calculates a large array of prime numbers. I defined a protocol that both the Swift and the Objective-C version of the calculate object are supposed to conform to. The objects are both singletons, so I created a typical singleton access method in Objective-C: + (NSObject <CalcPrimesProtocol> *) sharedInstance; The whole protocol looks like this: #import <Foundation/Foundation.h> @class ComputeRecord; typedef void

How to create class methods that conform to a protocol shared between Swift and Objective-C?

一曲冷凌霜 提交于 2019-11-26 08:35:11
问题 I\'ve been learning Swift lately. I decided to write a hybrid Swift/Objective-C app that did compute-intensive tasks using the same algorithm implemented in both languages. The program calculates a large array of prime numbers. I defined a protocol that both the Swift and the Objective-C version of the calculate object are supposed to conform to. The objects are both singletons, so I created a typical singleton access method in Objective-C: + (NSObject <CalcPrimesProtocol> *) sharedInstance;