Objective-C 类名带有 +
类名(如 UIImage+Something
或 UIImageView+Somethingelse
)是否意味着它的行为类似于自定义 UIImage
或 UIImageView?
Does a class name, say UIImage+Something
or UIImageView+Somethingelse
, mean that it acts like a custom UIImage
or UIImageView
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您正在查看类别的文件名,而不是类的文件名。 Objective-C 中的类名或任何其他标识符中不允许使用加号字符
+
。Objective-C 类别是一种向您不一定拥有源代码的类添加方法(但不是实例变量)的方法。例如,如果您经常想要制作 UIImage 的颠倒副本,则可以使用类别将
upsideDownImage
方法添加到 UIImage 类中。通常将此代码保存在名为
UIImage+UpsideDown.m
的文件中(带有随附的头文件UIImage+UpsideDown.h
)。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
).这是使用 Objective-C 类别来扩展类的功能时的命名约定。请参阅文章:http://macdevelopertips.com/objective-c/objective-c -categories.html 以获得更好的解释。
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.