This article is divided into two parts:
1. Error and exception handling 2, assertions in Swift
1. Error and exception handling
In OC development, we usually set the error to nil
Nserror *= [Data writetofile:path options:options Error: &ERROR]; if (Error) { // wrong message }
In the vast majority of cases, this method does not have any errors, so we set the error directly to nil. But it may not be possible to debug when something goes wrong. The introduction of the exception mechanism in Swift makes our old programmers feel very kind, because we use them frequently in Java or C #. However, unlike Java, Swift uses the Do...catch syntax, and then modifies it with a try when it is possible to run out of exception, while Java directly places the block of code that might throw an exception into the Try...catch. Swift is faster than Java to lock the number of lines of the exception code, while Java may need a step-by-step debugging to discover that the sentence in Try...catch has been wrong, but I do not think it is an advantage, if the same exception is thrown in many places in Swift, Each sentence is modified to cause the code to become abnormally bloated.
enumLoginerror:errortype { CaseUsernotfound, Userpasswordnotmatch}func login (user:string, password:string) throws {if 1<2 { ThrowLoginerror.usernotfound}if 2>1 { ThrowLoginerror.userpasswordnotmatch} print ("Login successfully.")} Do { TryLogin"Onevcat", Password:"123")} Catchloginerror.usernotfound {print ("Usernotfound")} CatchLoginerror.userpasswordnotmatch {print ("Userpasswordnotmatch")}
2. Assertions in Swift
Assertions (assertion) are generally used in COCOA development to check whether input parameters meet certain conditions and "judge" them to improve communication efficiency among developers.
When judging whether certain input parameters meet the conditions, we programmers usually use the IF condition control to detect, if we encounter the situation can not continue to return or run out of error in advance. However, this results in additional overhead for the runtime, and it is better to use assertion processing for scenarios such as determining whether an input parameter satisfies a condition.
The assertion method provided to us in Swift:
Func assert (@autoclosure condition: ()defaultdefault Default )
To give a simple example, for example, our age cannot be less than 0 years old:
Func convertage (age:int), Int { 0" input age cannot be less than 0 years old ") return0= convertage (-ten)// Run-time error: // Assertion failed: // The age of entry must not be less than 0 years
Assert the most basic function:
When encountering an unhandled input, the run generates an error and throws our preset information to alert the engineer who called the code.
Features of assertions:
Similar to our custom NSLog, it is a development-time feature that works only when Debug is compiled and is not compiled at run time, so it does not consume runtime performance. These features make assertions known as debugging decisions that are well suited for programmers during the debugging development phase, and we do not need to deliberately remove these assertions when the code is released.
If we want to force the assertion to be disabled in Debug mode, in Build Setting for the corresponding target, we add the-assert-c in the other swift flags in swift Compiler-custom flags Onfig Release to force the assertion to be disabled, or-assert-config Debug to force the assertion to be enabled. (This is not recommended.)
Note: The assertion function Nsassert in OC has been removed from Swift.
Exception handling and assertion of swift development (i)