때때로 Objective-C 코드는 Try/Catch 구조를 사용하는 iOS 조각을 볼 수 있습니다. http://docs.xrtml.org/2-1-0/pubsub/ios/ortcclient.htmlNSError 테스트 대신 Try/Catch를 사용하는 이유
- (void)viewDidLoad
{
[super viewDidLoad];
// Instantiate OrtcClient
ortcClient = [OrtcClient OrtcClientWithConfig:self];
// Post permissions
@try {
NSMutableDictionary* myPermissions = [[NSMutableDictionary alloc] init];
[myPermissions setObject:@"w" forKey:@"channel1"];
[myPermissions setObject:@"w" forKey:@"channel2"];
[myPermissions setObject:@"r" forKey:@"channelread"];
BOOL result = [ortcClient saveAuthentication:@"http://ortc-developers.realtime.co/server/2.1/" isCLuster:YES authenticationToken:@"myAuthenticationToken" authenticationTokenIsPrivate:NO applicationKey:@"myApplicationKey" timeToLive:1800 privateKey:@"myPrivateKey" permissions:myPermissions];
if (result) {
// Permissions correctly posted
}
else {
// Unable to post permissions
}
}
@catch (NSException* exception) {
// Exception posting permissions
}
// Set connection properties
[ortcClient setConnectionMetadata:@"clientConnMeta"];
[ortcClient setClusterUrl:@"http://ortc-developers.realtime.co/server/2.1/"];
// Connect
[ortcClient connect:@"myApplicationKey" authenticationToken:@"myAuthenticationToken"];
}
왜 '일반'코코아 터치 코드가하는 것처럼 그냥 saveAuthentication:isCLuster:authenticationToken:...
방법에서 NSError (간접) 반환을 확인하지 수, 이러한 구조를 사용에서
는 예를 들어이 예 ? 예를 들어 JSON 읽을 때 : 일반적으로
NSError *error = nil;
id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
if (error == nil){
NSLog(@"%@", result);
}else{
NSLog(@"%@", [error localizedDescription]);
}
아이러니하지만 JSON 직렬 가능하지 않은 데이터가 주어진다면 NSJSONSerialization은 실제로 예외를 throw합니다. 또한 오류가 채워지지 않습니다. –