Correct Structure to check for Errors using NSError

前端 未结 4 1822
囚心锁ツ
囚心锁ツ 2021-02-02 01:59

I\'m coding up various routines and I\'m trying my best to keep it neat and refactored.

Methods I\'m creating are starting to look similar to this code:

         


        
4条回答
  •  情话喂你
    2021-02-02 02:35

    I think the real question is how much granularity you need in your error handling. Your code is fundamentally correct - but you can clean things up by checking for errors less frequently, or using a catch-all approach like teabot mentioned in his answer with NSException. A lot of the built-in functions that return errors (like NSFileManager's moveItemAtPath:) return a BOOL in addition to a providing an NSError object - so you could also use nested if statements if you don't need the NSError information.

    All and all though, there's really not a great way to do this. If your code is long, KVO might be a cool solution. I've never tried it for a situation like this, though.

提交回复
热议问题