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

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

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.