简体   繁体   English

在Objective-C中初始化类别

[英]Initialising a category in objective-c

I'm writing a category for UITextField to include validation. 我正在为UITextField写一个类别以包括验证。 I wish to change the text field's visual according to validation state (such as having an approved icon as its right view). 我希望根据验证状态更改文本字段的视觉效果(例如,将批准的图标作为其右视图)。 For this, I keep a validation state property and wish to update the visual on its setter. 为此,我保留了一个验证状态属性,并希望更新其设置器上的外观。

Here's what I have (UITextField+Validation.h) 这是我所拥有的(UITextField + Validation.h)

@interface UITextField (Validation)

// Validator registration
- (void)addValidator:(id<HyValidator>)validator;

// Validation
- (void)validate;

@end

UITextField+Validation.m UITextField + Validation.m

@interface UITextField (Validation_Private)

@property (nonatomic, strong) NSMutableArray * validators;
@property (nonatomic) HyValidationState validationState;

@end

@implementation UITextField (Validation_Private)

- (NSMutableArray*)validators
{
    if (self.validators == nil) {
        self.validators = [[NSMutableArray alloc] init];
    }
    return self.validators;
}

- (void)setValidators:(NSMutableArray *)validators
{
    self.validators = validators;
}

- (HyValidationState)validationState
{

}

- (void)setValidationState:(HyValidationState)validationState
{

}

- (void)addValidator:(id<HyValidator>)validator
{
    [[self validators] addObject:validator];
}

- (void)validate
{

}

@end

The question is: how do I initialise validators and validationState ? 问题是:如何初始化validatorsvalidationState

Don't use categories for this. 请勿为此使用类别。 Subclass instead. 子类代替。 Or, better yet, use the UITextField's delegate to do the validation, as intended. 或者,更好是,按预期使用UITextField的委托进行验证。

Using categories to extend the behavior of existing system classes is generally considered to be bad design. 使用类别扩展现有系统类的行为通常被认为是错误的设计。

By using delegation, you can decouple input validation from a specific input class and, thus, your validation can be easily re-used across other input mechanisms. 通过使用委托,您可以将输入验证与特定输入类解耦,因此,您的验证可以轻松地在其他输入机制之间重复使用。

You want to add a storage to your class UITextField (simple ivar to hold the data). 您想将存储添加到类UITextField(用于保存数据的简单ivar)。 Since you don't have the code you can't extend the class. 由于您没有代码,因此无法扩展该类。 However in objective C you can achieve this using associated reference. 但是,在目标C中,您可以使用关联的参考来实现。 ObjC Runtime comes handy helping you to attach a storage to your class and make you interact with the storage as if it was built in within the class. ObjC Runtime非常方便,可以帮助您将存储连接到您的班级,并使您与该存储区进行交互,就好像它是在班级中内置的一样。

An example of how to achieve this is found in Ole Begemann blog here http://oleb.net/blog/2011/05/faking-ivars-in-objc-categories-with-associative-references/ 有关如何实现此目标的示例,请参见以下网址的Ole Begemann博客: http: //oleb.net/blog/2011/05/faking-ivars-in-objc-categories-with-associative-references/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM