objective-c-category

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

Add rounded corners to all UIImageViews

こ雲淡風輕ζ 提交于 2019-11-26 22:30:37
问题 I would like to add some rounded corners to all of the UIImageViews in my project. I have already got the code working, but am having to apply it to every image; should I subclass UIImageView to add this? If so, can someone give me some pointers as to how to do this? Here is the code - (void)viewDidLoad { [super viewDidLoad]; NSString *mainpath = [[NSBundle mainBundle] bundlePath]; welcomeImageView.image = [UIImage imageWithContentsOfFile:[mainpath stringByAppendingString:@"/test.png"]];

Class extension vs class category

空扰寡人 提交于 2019-11-26 15:07:51
问题 Class extensions @interface Class () are a lot more powerful and can inject variables into the class. Categories @interface Class (Category) can't. What other differences are there, and when should one use a category over a class extension? 回答1: The main difference is that with an extension, the compiler will expect you to implement the methods within your main @implementation , whereas with a category you have a separate @implementation block. So you should pretty much only use an extension