Learn the Swift from the Start learning Note (Day--cocoa) error handling mode

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.