2009年11月3日 星期二

@selector in NSArray

除了之前學習到的 Class in NSArray 外,今日嘗試製作 Selector in NSArray。方法同樣很簡單:

- (void)runAllTest:(id)sender {
NSArray *menuList = [[NSArray alloc] initWithObjects:
@"test01:",
@"test02:",
@"test03:",
@"test04:",
nil];

NSString *currentTestName = [menuList objectAtIndex:currentTest];
[self addLog:[NSString stringWithFormat:@"[%@]...", currentTestName]];

// Get the selector of next test
SEL currentSelector = NSSelectorFromString(currentTestName);
int assertResult = (int)[self performSelector:currentSelector];

if (assertResult == 0) {[self addLog:@"OK\n"];}
else {[self addLog:[NSString stringWithFormat:@"ERROR %d\n", assertResult]];}

// Test complete
currentTest++;

// Stop if all tests have been ran
if (currentTest >= [menuList count]) {
[self stopTest:sender];
currentTest = 0;
return;
}

// Set timer for next test
timer = [NSTimer scheduledTimerWithTimeInterval:(0.5f) target:self selector:@selector(runAllTest:) userInfo:nil repeats:NO];
}