Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Swift 2.0 brief     - exp / imp Export Import version of the problem and the ORA-6550 error (Database)

- Open container cluster management system architecture and components introduced Kubernetes (Server)

- Git Advanced Tutorial (Linux)

- Ubuntu Telnet service settings (Linux)

- MySQL how to export files with the date format (Database)

- Java input and output common class Scanner (Programming)

- Rails project prepared some tips small experience (Linux)

- 11 examples in Linux df command (Linux)

- SQLite3 simple operation (Database)

- ElasticSearch - Basic Concepts (Server)

- Installation Strongswan: on a Linux IPsec-based VPN tool (Linux)

- Oracle inline view updates problems encountered (Database)

- Lua regex (string function) (Programming)

- Red Hat Enterprise Linux Server 6.5 installation GCC 4.9.2 (Linux)

- About Linux iptables firewall interview questions and answers (Linux)

- Unable to start the network after restart clone a virtual machine (Linux)

- Based LNMP + uWSGI deploy Django applications (Server)

- File easier to compare tools under Linux (Linux)

- Get basic information about Linux server script (Server)

- MySQL Server Time Synchronization Problem (Database)

 
         
  Swift 2.0 brief
     
  Add Date : 2018-11-21      
         
         
         
  Read a lot of articles describes Swift 2.0, the feeling Appcoda this introduction more clearly, and easily learn a little translation, the original English look here.

What's New in Swift 2.0: A Brief Introduction

A year ago, Apple iOS and OSX developers to bring a new programming language Swift, when Apple's vice president announced at WWDC moment, I and many developers, excited. As propaganda faster, more secure language, Swift has grown into the most popular languages. At this year's WWDC Apple introduced the Swift 2, I was lucky enough to attend the WWDC conference, here I share some of the new Swift updates.

We're stepping on the gas this year with Swift 2. We think Swift is the next big programming language, the one we will all do application and systems programming on for 20 years to come. We think it should be everywhere and used by everyone .

Craig Federighi, Apple's senior vice president of Software Engineering

I noticed the biggest applause WWDC venue twice, once Apple announced Xcode 7 support UI testing, the Swift is another open-source, which will be a big event, later this year, Apple will develop the Swift the source code to the public, including some of the basic compiler and libraries, which are under the OSI-compliant license. Apple also will allow Swift to support Linux, developers can use to write the Swift system is applied. This shows that Apple has finally started to make efforts to promote the development of the language that newborn door.

Remove these exciting news, Swift 2 introduces some new features, such as error handling (error handling), protocol extension (protocol extensions) and check availability (availability check), let's take a detailed introduction.

Error Handling

Program always error when the function is wrong, if we can figure out what went wrong, help to understand why it failed. Swift version 1 lack of appropriate error handling mode, Swift 2, finally joined the exception handling model, use try / throw / catch keywords.

Imagine a car engine, the engine failed to start because of the following reasons:

No oil (No fuel)
Oil spills (Oil leakage)
Battery Low (Low battery)
In Swift, the error can be seen as follow ErrorType protocol type. Accordingly, you can create a compliance ErrorType enumeration model to represent the above error conditions:

enum CarEngineErrors: ErrorType {
    case NoFuel
    case OilLeak
    case LowBattery
}
To create a function can throw an exception, you can use the throws keyword:

func checkEngine () throws {
}
To throw an error in a function, you can use the throw statement The following example demonstrates the check engine:

let fuelReserve = 20.0
let oilOk = true
let batteryReserve = 0.0

func checkEngine () throws {
    guard fuelReserve> 0.0 else {
        throw CarEngineErrors.NoFuel
    }

    guard oilOk else {
        throw CarEngineErrors.OilLeak
    }

    guard batteryReserve> 0.0 else {
        throw CarEngineErrors.LowBattery
    }
}
Swift 2 guard key is to enhance the flow of control (control flow) launched. When execution control branches to guard statement first checks followed by a conditional statement if the condition is false, the else part will be executed, the example above condition is false executed throw statement throws an exception.

To call throwing function, you need to try before the function name keywords into:

func startEngine () {
    try checkEngine ()
}
If you write the above code in Playgrounds, you will get an error. Error handling mechanism Swift requirements you must use the do-catch statement to catch any errors and handle them.

The following function specifies the relevant error after capturing print-related error messages:

func startEngine () {
    do {
        try checkEngine ()
        print ( "Engine started", appendNewline: true)
    } Catch CarEngineErrors.NoFuel {
        print ( "No Fuel!")
    } Catch CarEngineErrors.OilLeak {
        print ( "Oil Leak!")
    } Catch CarEngineErrors.LowBattery {
        print ( "Low Battery!")
    } Catch {
        // Default
        print ( "Unknown reason!")
    }
}
Each catch clause matches the specific error, then the error is specified after the capture should do. In the above example, batteryReserve variable is set to 0, execute startEngine in this case () ,. LowBattery error will be thrown.

The batteryReserve to 1.0, there is no such error when launched the car.

Similar to the switch statement, Swift error handling model 2 also requires complete, which means you have to handle all possible errors. That is why we want to include a catch last match without any pattern

If you want to learn more about Swift's error handling, I recommend that you read Apple's official documentation

No More println ()

See here, you may have noticed that println () function missing in Swift 2, we can use the print () to print out. Apple will println () and print () combined. If you want to print a new line, you can set the parameter to true appendNewline

print ( "Engine started", appendNewline: true)
Protocol Extensions

Swift in the first edition, you can use extensions to existing classes, structures, enumerated types to add new features. Swift 2 allows developers to extensions brought on protocol type. By Protocol Extensions, you can add a protocol to comply with class properties or functions. When you want to extend the protocol functionality will become very useful.

Or through an example to illustrate it, and create a Awesome protocol that provides a awesomeness (literally) the percentage return, as long as any object that implements the method of this agreement can be seen as follows:

protocol Awesome {
    func awesomenessPercentage () -> Float
}
Now we declare two classes Awesome follow this agreement, each class implements the protocol method:

class Dog: Awesome {
    var age: Int!
    func awesomenessPercentage () -> Float {
        return 0.85
    }
}

class Cat: Awesome {
    var age: Int!
    func awesomenessPercentage () -> Float {
        return 0.45
    }
}

let dog = Dog ()
dog.awesomenessPercentage ()

let cat = Cat ()
cat.awesomenessPercentage ()
If you are in demo Playground

If you want to add a property to Awesome awesomenessIndex agreement, we will use awesomenessPercentage () method to calculate the index awesomeness:

extension Awesome {
    var awesomenessIndex: Int {
        get {
            return Int (awesomenessPercentage () * 100)
        }
    }
}
By creating an extension on the protocol, all follow the protocol class will have access immediately awesomenessIndex indexed.

Availability Checking

Each time the developer must build App and have different versions of iOS to fight. You always want to use the latest API, but when APP ye love running older versions of iOS, will produce some errors. Before Swift 2, none of the iOS version can persist ways, such NSURLQueryItem class available only on iOS 8, if you are using in previous iOS versions, you will immediately get an error and crash out, in order to prevent this occurrence of an event, you can perform the following checks:

if NSClassFromString ( "NSURLQueryItem")! = nil {
    // IOS 8 or up
} Else {
    // Earlier iOS versions
}
This is a class to check whether there is a way, starting from the Swift 2, API support check availability at a particular version, you can easily define a usable condition and perform the iOS version relevant in the corresponding code block specific codes:

if #available (iOS 8, *) {
    // IOS 8 or up
    let queryItem = NSURLQueryItem ()

} Else {
    // Earlier iOS versions

}
do-while is now repeat-while

Classic do-while loop is now renamed to repeat-while, seemingly semantic more clear:

var i = 0
repeat {
    i ++
    print (i)
} While i <10
Summary

I hope you are happy to go read the Swift official profile 2, there are many things I did not mention in this article, such as comments, etc. Markdown format, you can also go to watch the video to learn more about Swift WWDC 2 content. At this point, there are still quite a number of companies to develop iOS Objective-C as the main language, maybe you are using OC. But I strongly believe that Swift is the way of the future. In fact, WWDC 2015 all sessions are in use Swift, so if you have not learned Swift, now begin to take action now.
     
         
         
         
  More:      
 
- Setting the RedHat9 Intrusion Detection System (Linux)
- Build ASP.NET 5 development environment in Ubuntu (Server)
- Ubuntu 14.04 Docker installation (Linux)
- How to contribute code to the GNU Project (Linux)
- Using a proxy method under Linux terminal (Linux)
- CentOS-based Kickstart automated installation practice (Linux)
- Configuring VMWare FreeBSD9.2 remote debugging kernel source code (Linux)
- CentOS7 installation hardware monitoring for Zabbix enterprise applications (Server)
- Smack 4.1.x Upgrade Guide (Linux)
- Ubuntu uses conky add desktop control (Linux)
- Python method to traverse folders and file read and write (Programming)
- RHEL7 unattended automatic installation DHCP + TFTP + SYSLINUX + TFTP + Kickstart (Linux)
- CentOS / Linux SELinux Close (Linux)
- Linux hybrid character device (Linux)
- How to use SVN to manage our source code (Server)
- Python2.7.7 source code analysis (Programming)
- Running the open-source Swift under Linux platform (Linux)
- How to install Linux on a MacBook Pro Retina (Linux)
- B-tree - ideas and implementation of C language code (Programming)
- Installation Elementary OS Freya 20 things to do (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.