1
我有類ABCD.m,如下如何訪問另一個.m文件的.m文件的屬性?
**ABCD.m**
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) NSString *string;
- (void) firstMethod;
- (void) setTheButtonWithBool:(BOOL)var1 withString:(NSString *)var2;
-(void) firstMethod {
// Alloc init button
self.button.enabled = NO;
}
- (void) setTheButtonWithBool:(BOOL)var1 withString:(NSString *)var2 {
self.button.enabled = var1;
self.string = var2;
}
還有另一類Test.m(XCTestCase的子類)寫單元測試用例ABCD.m
**Test.m** //Sub-class of XCTestCase
//Extension
@interface ABCD.m()
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) NSString *string;
- (void) firstMethod;
- (void) setTheButtonWithBool:(BOOL)var1 withString:(NSString *)var2;
@end
@interace Test : XCTestCase
- (void)testSomeMethod {
ABCD *abcd = [ABCD alloc] init];
BOOL *var1 = YES;
NSString *var2 = @"StackOverFlow";
[abcd firstMethod];
[abcd setTheButtonWithBool:var1 withString:var2];
nslog(@"Result1 :%hhd", self.abcd.button.isEnabled); -----
nslog(@"Result2: %@", self.abcd.string); -----
// Assert statement
}
OUTPUT:
結果1:NO
Resul t 2:StackOverFlow
當我設置屬性'字符串'它被設置爲'StackOverFlow'。但對於UIButton屬性「按鈕」,它不會被設置爲「否」。 爲什麼不能設置UIButton的'enabled'屬性,因爲我可以從Test.m類設置ABCD.m的NSString
首先可以檢查按鈕對象是否被初始化。 因爲在初始化按鈕的樣本中沒有可用的代碼。 – CoolMonster