The SWIFT team has been optimizing to prepare you for the migration to swift2 in the fall.
First, error handling
Exception handling, not nserror objects and double pointers.
You can use throws to specify a method to throw an error.
Call the Do try catch multiple keywords to catch and handle errors.
A reference to the Nserror object, inout
The method assigns a value to the error variable, passing in a nil here to completely ignore the error, or pass in Nserror but never check it.
Enum Drinkerror:errortype
{
Case Nobeerremainingerror
}
Func Drinkwitherror () throws
{
If Beer.isavailable ()
{
party!
}else{
Throw Drinikerror.nobeerremainingerror
}
}
Func Trytodrink ()
{
do{
Try Drinkwitherror ()
}
catch{
Print ("Could not drink beer!:[")
Return
}
}
1. Create an enum that inherits ErrorType
2, use throws keyword, flag any function can throw an error.
3, throws an error, will be caught in the section4.
4, in the Do block contains any code that can throw an error, rather than other languages similar to the try fast, and then you add a try keyword to the function is called before the function can throw an error.
Binding
In Swift 1.2 lost the doom of the Pyramids and was able to test multiple bindings in a line of code optionals;
If let pants = Pants,frog = frog{
Good Stuff here!
}
Check for some optionals that have no value to exit early, which is the guard statement provided by Swift 2.
Guard Let pants = Pants,frog = Frog else{
Sorry,no Frog pants Here
Return
}
Using guard means that you can execute the optional binding, and if the condition fails to provide a block of code to run in else, you can continue execution, optiional frog and pants are unwrap within the scope
Use Guard to specify something that you want to get state instead of checking for error conditions to make your code more concise.
Protocol Extensions
Protocol-oriented programming language
In Swift1, the protocol is like an interface where you can specify properties and methods, classes, structs, and enumerations to follow it.
In swift2, you can extend the protocol to add default implementations to properties and methods that you have previously been able to add new methods to a string or array in the class and struct
You can add these to the protocol to make your application more widespread.
Extension customstringconvertible{
var shoutdescription:string{
Return "\ (self.description.uppercaseString)!!!"
}
}
Let greetings = ["Hello", "Hi", "well"]
Prints Hello,hi,yo yo yo
Print ("\ (greetings.description)")
Prints Hello,hi,yo yo yo
Print ("\ (greetings.shoutydescription)")
The protocol is named Customstringconvertible most of the foundation objects follow the printable protocol, and with the protocol extension, you can extend the system with custom functionality.
Rather than adding a small amount of custom code to many classes, structs, and enumerations, you can write a generic implementation that applies to different data types.
Using the map filter, it can be considered better to use them in a way than global functions.
Some new methods have been added to the collection type.
Map,filter,indexof
ET numbers = [1,5,6,10,16,42,45]
Swift 1
Find (Filter (map (numbers,{$0*2}), {$0%3 = = 0}), 90)
Swift 2
numbers.map{$0*2}.filter{$0%3 = = 0}.indexof (//return) 2
- The objective-c generic –apple has started labeling all objective-c code so that the swift type can obtain the correct type of optional. Working with Objective-c Generics also works well, giving swift developers a better type hint. If you want to have some Uitouch objects or array of strings, you'll get what you want instead of some anyobjects.
- Renaming syntax –println has been away from us for a year; now it's plain old print, and now it has a second parameter whose default value is set to true to decide whether to wrap. The Do keyword is primarily used for error handling, and the Do-while loop is now using Repeat-while. Similarly, a lot of protocol names have changed, for example: Printable changed to customstringconvertible.
- Migrator– has a lot of small grammatical changes, how do you make your code up to date? Swift 1-to-2 Migrator will turn code into the newest standard and change the syntax. This migrator smart to be able to update your code with new error handling, and update block annotations to new formatting styles!
- Open source! – One of the big news for the code farmer is that Swift will open source in the autumn when Swift 2 is released! This means that you can not only use it for iOS development, but more importantly to learn its source code. Not only that, it would be a good opportunity to delve into the source code, even contribute code for the project, and then leave your name in the Swift compiler commit history.
- Article quote freely address: O (∩_∩) o Thank you
Http://ios.jobbole.com/82099/?from=groupmessage&isappinstalled=1
This is just some simple example of all the publishing features; For more information, see WWDC session videos and the updated Swift programming Language book
If there are other people who remember that there are many changes between Swift's first beta and release 1.0, there will definitely be more features coming up in the future. Our team will continue to focus on all the updates and dig into exciting changes, so keep a close eye on tutorials, books and videos.
Swift 2 feature recording