Original articles, welcome reprint. Reprint Please specify: Dongsheng's Blog
The swift error handling mode in Swift 1.x and Swift 2.0 is different in both modes.
The Swift 1.x code error handling mode uses the Cocoa framework error handling mode, which is now used by Objective-c, and the Do-try-catch error handling mode is followed by Swift 2.0.
The following example code reads a string from a file into memory if you use the Swift 1.x error-handling mode code as follows:
var err:nserror? // define optional nserror? variables = nsstring (contentsoffile:filepath, &if err! = Nil { // determine if the ERR variable is still nil // Error Handling }
Nserror? must be an optional variable, because you want to initialize it to nil.
Determine if the ERR variable is still nil, if it is nil in the code
Let contents = nsstring (contentsoffile:filepath, &err)
No error occurred during method call, otherwise an error occurred.
The constructor for the above code, its swift syntax is defined as follows:
Init? (Contentsofurl url:nsurl, encoding enc:uint, error Error:nserrorpointer)
The last parameter of the constructor is Nserrorpointer (that is, the Nserror pointer), then we need to pass the ERR variable address (that is, &err),& is the address character in the actual call. The ERR variable is assigned a value if there is an error after the method call is complete.
Welcome to follow Dongsheng Sina Weibo @tony_ Dongsheng.
Learn about the latest technical articles, books, tutorials and information on the public platform of the smart Jie classroom
?
More Products iOS, Cocos, mobile design courses please pay attention to the official website of Chi Jie Classroom: http://www.zhijieketang.com
Luxgen Classroom Forum Website: http://51work6.com/forum.php
Learn Swift from zero learning Note (day--cocoa) error handling mode