Noting the general guidance re use of exceptions in objective-c (e.g. throwing an exception in objective-c/cocoa) I'm still not quite sure for this case:

is throwing an exception in an objective-c method when a required input parameter is nil best practice/ok?

If no, what approach would be typical then here in objective-c?

有帮助吗?

解决方案

That would be a valid exception, or even an assert (which in turn will throw an exception). there is the NSParameterAssert macro defined already that will do what you want.

其他提示

An NSAssert is something which is more meant for the developers own convenience. Developers use it for checking the assumptions/conditions etc. One thing to note is that NSAssert will not be compiled into your code in a release build.

You do @throw NSException for your situation as they are meant only for these situations when you want to handle the cases for invalid inputs to your code at runtime.

NSErrors is also another facility provided by Apple. Its generally used for unwanted conditions which are not programming errors (say bluetooth hardware is defected and you bluetooth related application is dependent on that)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top