objective-c-category

What's the difference and use of categories and inheritance? [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 13:38:59
Possible Duplicate: Difference between inheritance and Categories in Objective-c When should I use subclassing and when should I use categories? Subclass when you want to extend the functionality utilized by the base. @interface MyObject: NSObject<SomeProtocol> Add a category when you want to add a convenience method to code you may not control. @interface UIView (MyViewAdditions) - (void)recursiveEnumerateSubviewsUsingBlock:(void (^)(UIView *view, BOOL *stop))block; 来源: https://stackoverflow.com/questions/13344075/whats-the-difference-and-use-of-categories-and-inheritance

Objective-C class names with a +

有些话、适合烂在心里 提交于 2019-12-01 18:51:24
Does a class name, say UIImage+Something or UIImageView+Somethingelse , mean that it acts like a custom UIImage or UIImageView ? I think you are looking at the file names of Categories, not Classes. The plus character + is not allowed in class names or any other identifier in Objective-C. An Objective-C category is a way of adding methods (but not instance variables) to a class you don't necessarily have the source to. For example, if you frequently want to make upside-down copies of UIImages, you can use a category to add a upsideDownImage method onto the UIImage class. It's common to save

What's wrong with using a category on NSObject to provide a default protocol implementation?

安稳与你 提交于 2019-12-01 16:23:48
I've been looking for a way to use optional protocol methods and have clean code. In other words: 1: No respondsToSelector: calls all over my code 2. Should work for any method signature, so a category method on NSObject making the check and calling performSelector: is out (and NSInvocation has problems cooperating with ARC) 3: This solution , IMO, pretends to be universal but has all the drawbacks of 1 I eventually came up with this idea: @protocol MyProtocol <NSObject> @optional -(void)optionalMethod; @end @interface ClassA : NSObject <MyProtocol> @end @implementation ClassA -(void

Can category methods be overridden? IOS

扶醉桌前 提交于 2019-11-30 17:54:58
I am trying to plan how to add a couple methods to all instances of certain objects. I think adding a category to the parent object(UIViewController) would work for what I want to do, but can I override a method added this way? Most of the time the subclasses will use the default methods but I do know I will need to override the method at least once. Also what other methods should I consider for what I am trying to do? Example of what I am trying to do: I have a set of Objects that act like pages of a journal. These pages are subclasses of UIViewControllers. I want to add methods for loading,

Is this a sane Objective-C Block Implementation?

女生的网名这么多〃 提交于 2019-11-30 15:30:40
I wanted a variation of NSRegularExpression's – stringByReplacingMatchesInString:options:range:withTemplate: method that takes a block instead of a template. The return value of the block would be used as the replacement value. This is more flexible than a template, as you can imagine. Sort of like using the /e modifier in Perl regular expressions. So I wrote a category to add the method. This is what I came up with: @implementation NSRegularExpression (Block) - (NSString *)stringByReplacingMatchesInString:(NSString *)string options:(NSMatchingOptions)options range:(NSRange)range usingBlock:

Can Objective-C protocols and categories be inherited?

南楼画角 提交于 2019-11-30 13:17:06
问题 I am little confused about some concepts around Objective-C protocols and categories. Can protocols and categories be inherited by subclasses in Objective-C? 回答1: Categories are collections of methods that are added to a class at run time. Because Objective-C uses dynamic binding, this means that the methods defined in a category are available to the class and all of its subclasses. Specifically selectors are bound to methods at the point where they are invoked, not during compilation or when

How to call original implementation when overwriting a method with a category?

故事扮演 提交于 2019-11-30 09:59:33
I try to figure out how things really work. So I thought when I would overwrite certain methods using categories, I would get interesting NSLogs. @implementation UIView(Learning) - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { NSLog(@"-hitTest:withEvent: event=%@", event); return [self hitTest:point withEvent:event]; } @end super and self don't work here. Is there a way to call the original implementation of -hitTest:withEvent:? What I want is an NSLog every time -hitTest:withEvent: is called on an UIView. It's just for personal learning purposes. I want to see the event

Can Objective-C protocols and categories be inherited?

天涯浪子 提交于 2019-11-30 06:36:52
I am little confused about some concepts around Objective-C protocols and categories. Can protocols and categories be inherited by subclasses in Objective-C? Categories are collections of methods that are added to a class at run time. Because Objective-C uses dynamic binding, this means that the methods defined in a category are available to the class and all of its subclasses. Specifically selectors are bound to methods at the point where they are invoked, not during compilation or when the program first loads. Categories are added to classes when they (the categories) are loaded. Protocols

NSManagedObject subclass property in category

烂漫一生 提交于 2019-11-30 05:53:42
问题 The NSManagedObject subclass property are created in its category file, which is very wired since category can only have method. The details are as below: (1).I have created an entity called BibleAudio in .xcdatamodeld file with several attributes as below. (2). the xcode generated objective c files are "BibleAudio + CoreDataProperties.h", "BibleAudio + CoreDataProperties.m" and "BibleAudio.h", "BibleAudio.m" as following: (3). within "BibleAudio + CoreDataProperties.h", BibleAudio's

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