oclint規則 Cocoa

Cocoa

重寫isEqual必須重寫Hash MustOverrideHashWithIsEqual

Since: 0.8

當 isEqual 方法被重寫, hash 方法也應該被重寫.

定義類: oclint-rules/rules/cocoa/ObjCVerifyIsEqualHashRule.cpp

Example:

@implementation BaseObject

- (BOOL)isEqual:(id)obj {
    return YES;
}

/*
- (int)hash is missing; If you override isEqual you must override hash too.
*/

@end

必須調用超類 MustCallSuper

Since: 0.8

當一個類使用 __attribute__((annotate("oclint:enforce[must call super]"))) 註解的時候, 他的所有實現(包括他自己和子類)都必須調用超類的實現

定義類: oclint-rules/rules/cocoa/ObjCVerifyMustCallSuperRule.cpp

Example:

@interface UIView (OCLintStaticChecks)
- (void)layoutSubviews __attribute__((annotate("oclint:enforce[must call super]")));
@end

@interface CustomView : UIView
@end

@implementation CustomView

- (void)layoutSubviews {
    // [super layoutSubviews]; is enforced here
}

@end

驗證禁止引用VerifyProhibitedCall

Since: 0.10.1

當一個方法標記 __attribute__((annotate("oclint:enforce[prohibited call]"))) 註解,所有的引用都將被禁止。

定義類: oclint-rules/rules/cocoa/ObjCVerifyProhibitedCallRule.cpp

Example:

@interface A : NSObject
- (void)foo __attribute__((annotate("oclint:enforce[prohibited call]")));
@end

@implementation A
- (void)foo {
}
- (void)bar {
    [self foo]; // calling method `foo` is prohibited.
}
@end

驗證 Protected 方法 VerifyProtectedMethod

Since: 0.8

 protected 標記 Objective-C 雖然沒有語言級別的限制, 但是從設計角度有時候希望他只能被自己以及子類訪問. 此規則當開發人員調用的時候給予一個警告

定義類: oclint-rules/rules/cocoa/ObjCVerifyProtectedMethodRule.cpp

Example:

@interface A : NSObject
- (void)foo __attribute__((annotate("oclint:enforce[protected method]")));
@end

@interface B : NSObject
@property (strong, nonatomic) A* a;
@end

@implementation B
- (void)bar {
    [self.a foo]; // calling protected method foo from outside A and its subclasses
}
@end

子類必須實現 SubclassMustImplement

Since: 0.8

這條規則用來驗證抽象方法是被子類實現的

定義類: oclint-rules/rules/cocoa/ObjCVerifySubclassMustImplementRule.cpp

Example:

@interface Parent

- (void)anAbstractMethod __attribute__((annotate("oclint:enforce[subclass must implement]")));

@end

@interface Child : Parent
@end

@implementation Child

/*
// Child, as a subclass of Parent, must implement anAbstractMethod
- (void)anAbstractMethod {}
*/

@end
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章