What's the difference between objc
One useful tip I've been using for XCode is adding breakpoints on exceptions.
I was wondering why we need to add two breakpoints--one for objc_exception_throw
and one for [NSException raise]
.
What cases do one cover that the other doesn't?
You should only use a breakpoint on objc_exception_throw
. The method -[NSException raise]
calls objc_exception_throw
, so objc_exception_throw
covers all cases that -[NSException raise]
covers. The other way around is not true: The @throw
directive is compiled to call objc_exception_throw
directly. This method shows the difference:
- (void)throwAndCatch
{
@try {
NSException *exception = [[NSException alloc] initWithName:@"Test"
reason:@"test"
userInfo:nil];
@throw exception;
}
@catch (NSException *exception) {
NSLog(@"Caught");
}
}
When calling -throwAndCatch
, a breakpoint on -[NSException raise]
has no effect, while a breakpoint on objc_exception_throw
will work.
Here is what from Apple's Documentation about Exception:
An important difference between @throw and raise is that the latter can be sent only to an NSException object whereas @throw can take other types of objects as its argument (such as string objects). Cocoa applications should @throw only NSException objects.
Which means if you are implementing a Cocoa applciation project
and if you strictly follow that @throw only NSExeption
objects they are the same.
reference: Link
链接地址: http://www.djcxy.com/p/60598.html下一篇: objc有什么区别?