Objective-C class names with a +
Asked Answered
S

2

6

Does a class name, say UIImage+Something or UIImageView+Somethingelse, mean that it acts like a custom UIImage or UIImageView?

Squalene answered 14/7, 2011 at 0:4 Comment(0)
S
13

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 this code in a file named UIImage+UpsideDown.m (with an accompanying header file, UIImage+UpsideDown.h).

Strikebreaker answered 14/7, 2011 at 0:9 Comment(4)
It's generally a very bad idea to go adding new methods to Cocoa-provided classes, without ensuring that the name is reasonably unique. If you do want to do this, you should pick a prefix and put it at the beginning of your selector (just like you do with class names). If you don't, and Apple provides their own method with the same name in the future, your category will override it and likely cause bugs in code that thinks it's calling the Apple implementation of the method.Epagoge
Good point, Kevin, though I get the impression daidai is interpreting this code, not writing it.Strikebreaker
@Benzado: Just to clarify: It is purely a convention and an Objective-C category can be stored in any .m file, correct?Hoxsie
@ChristopherOezbek Correct.Strikebreaker
D
2

This is a naming convention when using an Objective-C Category to extend the functionality of a class. See the article: http://macdevelopertips.com/objective-c/objective-c-categories.html for a much better explanation.

Dunsany answered 14/7, 2011 at 0:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.