如何使用Objective-C类别

时间:2022-09-07 10:45:56

When you implement a category of a class in a file, will all the instances of that class be of the category by default?

在文件中实现类的类别时,默认情况下该类的所有实例都属于该类别吗?

I'm new to Objective-C and I'm trying to make my uneditable UITextView non-selectable. I came across this answer using a category: https://*.com/a/8013538/1533240

我是Objective-C的新手,我试图使我的不可编辑的UITextView不可选。我使用以下类别找到了这个答案:https://*.com/a/8013538/1533240

Which has the following solution:

其中有以下解决方案:

@implementation UITextView (DisableCopyPaste)

-(BOOL) canBecomeFirstResponder
{
    return NO;
}
@end

I added the snippet to my code, but it doesn't seem to be working in that I can still select the text. My declaration of the UITextView is the usual:

我将代码段添加到我的代码中,但它似乎没有起作用,我仍然可以选择文本。我对UITextView的声明是通常的:

titleLabel = [[UITextView alloc] initWithFrame:frame];

titleLabel = [[UITextView alloc] initWithFrame:frame];

I tried changing the declaration to [DisableCopyPaste alloc] but that didn't seem to work.. haha.

我尝试将声明更改为[DisableCopyPaste alloc],但这似乎不起作用..哈哈。

Thanks!

3 个解决方案

#1


20  

You misunderstand the point of categories. Categories add methods to an existing class. They must never be used to override existing methods. Doing so is undefined behavior (technically only undefined in one case, but you can't predict that case, so you must assume it applies).

你误解了类别的重点。类别将方法添加到现有类。它们绝不能用于覆盖现有方法。这样做是未定义的行为(技术上仅在一种情况下未定义,但您无法预测这种情况,因此您必须假设它适用)。

If you need to override methods, you must subclass, not use categories. See the top answer to the question you linked.

如果需要覆盖方法,则必须是子类,而不是使用类别。查看您链接的问题的最佳答案。

#2


6  

When you implement a category of a class in a file, will all the instances of that class be of the category by default?

在文件中实现类的类别时,默认情况下该类的所有实例都属于该类别吗?

Yes. If you create a category, the methods in that category are added to the class. For example, if you create a category on NSString that returns the checksum of a string, you can use that method on any instance of NSString.

是。如果创建类别,则该类别中的方法将添加到类中。例如,如果在NSString上创建一个返回字符串校验和的类别,则可以在NSString的任何实例上使用该方法。

I added the snippet to my code, but it doesn't seem to be working in that I can still select the text.

我将代码段添加到我的代码中,但它似乎没有起作用,我仍然可以选择文本。

Don't use categories to override existing methods.

不要使用类别来覆盖现有方法。

For one thing, it's bad form. You're effectively changing the behavior of the class in a way that the author didn't expect. For another thing, you can't count on the override to work -- the order in which categories are added to classes isn't defined, so you never know if some other category might come along and replace the method that you tried to replace. It's simply not reliable. If you need to override methods, create a subclass instead.

首先,它是糟糕的形式。您正在以作者没想到的方式有效地改变类的行为。另一方面,你不能指望覆盖工作 - 没有定义类别添加到类的顺序,所以你永远不知道是否可能出现其他类别并替换你试图替换的方法。它根本不可靠。如果需要覆盖方法,请改为创建子类。

#3


0  

What you need to do is to declare category in header .h file:

你需要做的是在header .h文件中声明类别:

such as:

@interface UITextView (DisableCopyPaste)
-(BOOL) methodName
@end

then in .m define as

然后在.m中定义为

@implementation UITextView (DisableCopyPaste)
-(BOOL) methodName
{
    return NO;
}
@end

You can do two thing,

你可以做两件事,

  1. You can write it in a class and import that to all classes you need this functionality.
  2. 您可以在类中编写它并将其导入到您需要此功能的所有类中。

  3. Or write these lines eachs .h and .m (respectively) you need it.
  4. 或者分别写下你需要的.h和.m这些行。

#1


20  

You misunderstand the point of categories. Categories add methods to an existing class. They must never be used to override existing methods. Doing so is undefined behavior (technically only undefined in one case, but you can't predict that case, so you must assume it applies).

你误解了类别的重点。类别将方法添加到现有类。它们绝不能用于覆盖现有方法。这样做是未定义的行为(技术上仅在一种情况下未定义,但您无法预测这种情况,因此您必须假设它适用)。

If you need to override methods, you must subclass, not use categories. See the top answer to the question you linked.

如果需要覆盖方法,则必须是子类,而不是使用类别。查看您链接的问题的最佳答案。

#2


6  

When you implement a category of a class in a file, will all the instances of that class be of the category by default?

在文件中实现类的类别时,默认情况下该类的所有实例都属于该类别吗?

Yes. If you create a category, the methods in that category are added to the class. For example, if you create a category on NSString that returns the checksum of a string, you can use that method on any instance of NSString.

是。如果创建类别,则该类别中的方法将添加到类中。例如,如果在NSString上创建一个返回字符串校验和的类别,则可以在NSString的任何实例上使用该方法。

I added the snippet to my code, but it doesn't seem to be working in that I can still select the text.

我将代码段添加到我的代码中,但它似乎没有起作用,我仍然可以选择文本。

Don't use categories to override existing methods.

不要使用类别来覆盖现有方法。

For one thing, it's bad form. You're effectively changing the behavior of the class in a way that the author didn't expect. For another thing, you can't count on the override to work -- the order in which categories are added to classes isn't defined, so you never know if some other category might come along and replace the method that you tried to replace. It's simply not reliable. If you need to override methods, create a subclass instead.

首先,它是糟糕的形式。您正在以作者没想到的方式有效地改变类的行为。另一方面,你不能指望覆盖工作 - 没有定义类别添加到类的顺序,所以你永远不知道是否可能出现其他类别并替换你试图替换的方法。它根本不可靠。如果需要覆盖方法,请改为创建子类。

#3


0  

What you need to do is to declare category in header .h file:

你需要做的是在header .h文件中声明类别:

such as:

@interface UITextView (DisableCopyPaste)
-(BOOL) methodName
@end

then in .m define as

然后在.m中定义为

@implementation UITextView (DisableCopyPaste)
-(BOOL) methodName
{
    return NO;
}
@end

You can do two thing,

你可以做两件事,

  1. You can write it in a class and import that to all classes you need this functionality.
  2. 您可以在类中编写它并将其导入到您需要此功能的所有类中。

  3. Or write these lines eachs .h and .m (respectively) you need it.
  4. 或者分别写下你需要的.h和.m这些行。