HI. I made an experiment about distributed object.
This exception handling is from a book:
I want to change it to use the for in loop.
But [listOfClients removeObject:currentClient]; leads to an error. I can't figure it out. I tried to split it into two steps, but still had problems.
Another question is that [currentClient respondsToSelector
selector(connectionForProxy)] never return YES. But it should. Testing it in gdb, I explicitly sent the message and it actually respond to connectionForProxy. Could someone give me an explanation?
I explicitly make it don't remove itself from the server list in AEClient.m , for the test of the block above.
Here's the whole codes:
This exception handling is from a book:
Code:
/*- (void)connectionDidDie:(NSNotification *)aNotification
{
NSConnection *deadConnection = [aNotification object];
int i;
for(i = [listOfClients count] - 1; i >= 0; i--)
{
id currentClient = [listOfClients objectAtIndex:i];
NS_DURING
if([currentClient respondsToSelector:@selector(connectionForProxy)])
{
if(deadConnection == [currentClient connectionForProxy])
{
[listOfClients removeObjectAtIndex:i];
NSLog(@"Removed client from client list.");
}
}
NS_HANDLER
[listOfClients removeObjectAtIndex:i];
NSLog(@"Removed client from client list.");
NS_ENDHANDLER
}
}*/
I want to change it to use the for in loop.
Code:
- (void)connectionDidDie:(NSNotification *)aNotification {
NSConnection *deadConnection=[aNotification object];
for(id currentClient in listOfClients) {
@try {
if([currentClient respondsToSelector:@selector(connectionForProxy)]) {
if([currentClient connectionForProxy]==deadConnection) {
[listOfClients removeObject:currentClient];
NSLog(@"Removed dead client: %@.", currentClient);
}
}
}
@catch (NSException * e) {
int i=[listOfClients indexOfObject:currentClient];
[listOfClients removeObjectAtIndex:i];
//[listOfClients removeObject:currentClient];
NSLog(@"%@. Removed dead client %@.", [e reason], currentClient);
}
@finally {
}
}
}
But [listOfClients removeObject:currentClient]; leads to an error. I can't figure it out. I tried to split it into two steps, but still had problems.
Another question is that [currentClient respondsToSelector
I explicitly make it don't remove itself from the server list in AEClient.m , for the test of the block above.
Here's the whole codes: