original articles, welcome reprint. Reprint Please specify: Dongsheng's blog
Swifterror handling mode, inswift1.xand theSwift 2.0are two different modes.
Swift 1.xCode error handling mode usesCocoaFrame error Handling mode, up to nowobjective-cThis processing mode is also used, andSwift2.0then adopted theDo-try-catcherror handling mode.
The following example code reads a string from a file into memory if you use the Swift 1.x The error handling mode code is as follows:
Import Foundation var err:nserror? Define optional nserror? variable let contents = NSString (Contentsoffile:filepath, êencoding:nsutf8stringencoding, Error: & ERR) If err! = Nil { //To determine if the ERR variable is still nil //error handling}
nserror? must be an optional variable, because you want to initialize it to a Nil .
JudgingErrwhether the variable is stillNil, if it is stillNilin the Code
Let contents = NSString (Contentsoffile:filepath, êencoding:nsutf8stringencoding, error: &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 isNserrorpointer(i.e.Nserrorpointer), then in the actual call we need to passErrvariable address (that is,&err),&is a fetch address character. When the method call is complete, if there is an errorErrThe variable is assigned a value.
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 ProductsIOS,Cocos, mobile Design course please pay attention to the official website of Chi Jie Classroom:http://www.zhijieketang.com
Smart-Jie Classroom Forum Website:http://51work6.com/forum.php
Learn the Swift from the Start learning Note (Day--cocoa) error handling mode