original articles, welcome reprint. Reprint Please specify: Dongsheng's blog
Swift error handling mode, in swift1.x and the Swift 2.0 are two different modes.
Swift 1.x Code error handling mode uses Cocoa Frame error Handling mode, up to now objective-c This processing mode is also used, and Swift2.0 then adopted the Do-try-catch error 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 .
Judging Err whether the variable is still Nil , if it is still Nil in the Code
Let contents =nsstring (Contentsoffile:filepath, encoding:nsutf8stringencoding, error: &err)
No error occurred during method call, otherwise an error occurred.
The constructor of the above code, its Swift The syntax is defined as follows:
Init? (Contentsofurl url:nsurl, encoding enc:uint, error error:nserrorpointer)
the last parameter of the constructor is Nserrorpointer (i.e. Nserror pointer), then in the actual call we need to pass Err variable address (that is, &err ), & is a fetch address character. 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
650) this.width=650; "title=" 00.png "src=" http://s5.51cto.com/wyfs02/M00/7C/F1/wKiom1bc8WqBmhhKAAAs2MBEZnc731.png "alt=" Wkiom1bc8wqbmhhkaaas2mbeznc731.png "/>
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
This article is from the "Dongsheng-ios Technical Consultant" blog, make sure to keep this source http://tonyguan.blog.51cto.com/701759/1748299
Learn the Swift from the Start learning Note (Day--cocoa) error handling mode