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

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

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.