Original address
Over the past few years, mobile apps have swept the world and have changed the way we use the Internet to work or relax. In order to create mobile applications, a variety of technologies emerged, and the development process began to treat them as first-class citizens. Although the move seems to be everywhere, its future is only just beginning. We are facing a new generation of mobile devices, such as wearable devices and a large number of mobile tools that make up the internet of things. We will face new user interfaces for displaying data and accepting commands. At the same time, we will see more and more companies really realize mobile first. All of this will affect the way we design, develop, and test software in the coming years.
Apple unveiled its new development language on WWDC 2014 swift--a programming language for IOS and OS x. Don't confuse Apple's Swift with the old parallel scripting language. Swift's goal is to make IOS and OS X development easier and more enjoyable. In this article, I'll explain what I think Swift has the most lethal five features , and why that's why, although these features are still in beta, it's worth a try.
Apple already has a programming language--objective-c. So why introduce another programming language? This is because, although Objective-c was very unique and advanced at the time of its creation, it now looks like it does not have the flavor of today's language. For example, on the consumer side, scripting languages like Ruby have been widely adopted, largely thanks to its clean syntax. In the enterprise domain, strongly typed (type-safe) languages with type inference capabilities are more popular. In order to introduce the functions of functional programming languages such as objects, Lambda expressions and other classical features, C # and Java (or Scala) have made a lot of effort. Objective-c has always lacked this kind of stuff, and Swift is trying to fill that void.
Objective-c is an object-oriented programming language that expands C. The operating system and its applications for writing IOS and Mac os X. It was the beginning of the 1980 's Blade Cox (Brad Cox) invented Objective-c in his company StepStone. Objective-c's popularity is attributed to the success of the IPhone.
This is not to say that objective-c is not a good programming language. In fact, it is an excellent language. But I do see enough space to succeed in replacing Objective-c. Furthermore, thanks to Swift's excellence, I think Swift will surely spread like wildfire.
Now, let's see what Swift has to offer. From a language perspective, Swift is very impressive. Apple has drawn on the advantages of Objective-c,javascript,python,scala and C # in these modern languages, building a very simple and powerful language. It's a perfect blend of object-oriented and functional programming paradigms--saying Swift is a functional language and a great extension. Let's take a look at the 5 most lethal features of Swift.
Grammar sugar
In a grammatical sense, Swift is gorgeous. It is a very simple and clean language, and it is also very readable, even if measured by current standards. You'll soon find that simplicity is a key element in designing a language. For example, the semicolon at the end of a statement that you are familiar with. Apple decided to use the semicolon as an optional, although it seems not very relevant, but it gives us a glimpse of Apple's efforts to keep the grammar clean as much as possible.
Other examples of simplicity include string insertions, and support for arrays and loops.
String insertion
var "Hello World"
"The message is \ (message)"
Var
"The sum is \ (A + b)" is
Cycle
var length = 10
for in 0..
Array
var list = ["a""B"]
List + = ["C""D"]
These are just a few examples of Swift's support for simplicity. It is important to note that you can still use the array class's "append" method to concatenate arrays, and Apple has gone an extra mile to build it as part of the language in order to demonstrate their goal of designing Swift.
If you want to learn Swift, then you can try Xcode 6, its code Live Preview (Playground) function is too cool to describe in words. The real-time preview feature lets you test your code in real time as you enter it. It executes everything you enter in the development environment, providing details about the value of the variable, the return value of the function call, and the number of times a particular code block was executed.
function is a one-class object
More and more languages use functions as top-class citizens and support higher-order functions. For example, the recently released Java 8 introduced a LAMBDA expression. Its concept is simple, that is, the function parameter is a function, at the same time, you can also use the function as the return value. This allows for more abstractions to be supported. For example, we can apply a filter function to an array that takes each entry in the array as a parameter and determines the entry by certain criteria to complete the filtering of the given array. The key to using a more general approach is to be able to accept parameters of the function type. Let's look at the syntax for defining a function.
The syntax of the SWIFT definition function is similar to a functional language such as the traditional Haskell, but not exactly the same. The left side of the arrow is the parameter and the type of the parameter, and the return value type on the right. In the following example, we want to filter a list of numbers and return a Bool type based on a given number. In this way, the function might look like this:
(Item:int), Bool
The meaning of this code is to accept a parameter of type INT and return a value of type Bool. Obviously, you can include multiple parameters, but it's not obvious that you can also return multiple values, which doesn't require creating a container object. In the following example, the function returns a tuple.
A function definition that filters integers might be:
Func bignumbersonly (item:int), Bool {
return item > 3
}
Now that we have created our own filter functions, let's look at the "filter" function that can accept the function parameter type.
var numbers = [1, 2, 3, 4, 5]
var bigones = Numbers.filter (bignumbersonly)
In this example, filter is a higher-order function because its parameter type is a function. In Swift, a function is also an object, which means that we can define an inline function:
var numbers = [1, 2, 3, 4, 5]
var inch return item > 3})
Or we can assign a function to a variable and use it later:
Define variables of type function
var inch return item > 3}
var inch return item = = 3}
Decide which one to apply at runtime
var result = Numbers.filter (onlythrees? threes:biggies)
Today, using functions as objects, allowing users to reference and transfer functions as if they were parameters, has become a graceful standard. And the simplicity of Swift's implementation is still a commendable place, and this core concept, combined with type inference, allows you to do more with less.
Strongly typed and type inference
In the area of enterprise development, we are very accustomed to using strongly typed (or type-safe) languages. Strongly typed languages often give us a little extra confidence because they can be checked for errors at compile time, and it would be a good experience if they could support type inference. For example, you can do this:
Without Type inference
var "Bar"
With Type inference
var "Bar"
Note that the second line of the statement does not declare the type of the variable. Because Swift knows that "bar" is a string, there is no need to explicitly define its type, and of course we can specify, as in the first statement. This does not seem to be particularly important, but it becomes interesting if the type of the function is inferred.
If we don't use types, how do we define the functions in the previous example? The following code shows the implementation of Swift:
$ would map to the first parameter, $ to the second ...
var return $ = = 3})
Nothing is more concise than this! If you have more than one parameter and want to reference the parameter by name, you can do this:
inch return a > B}
Generic type
Another very handy feature that Swift offers is generics. In the area of enterprise development, generics were first introduced into C #, and Java introduced this feature after gaining a lot of attention. Using generics allows developers to eliminate type conversions because the compiler is able to run specific type checks, which cannot be done for languages that do not support generics. While the introduction of generics into C # did create some controversy, it is now universally accepted by the C # and Java community.
Generics provide a way for us to defer the definition of a type, usually (but not limited to) the type of the parameter and the return value. It sounds complicated, but in fact it can be very easy to understand with a simple example.
At the moment of creating this function
I am not defining-T is. The actual
Type of T is deferred to the call
of the doNothing function.
Func doNothing (item:t), T {
return Item
}
When I call doNothing, I am implicitly
Setting the type of T to the type of the
Parameter I am sending. In the case,
An Array.
Notice How the compiler knows, the
Return type is an Array.
DoNothing ([1, 2, 3]). Count
Although the above is not a real example, it allows us to see the convenience of determining the type of elements in an array at compile time. The following is a simpler example of how the compiler knows that an array is a string type:
var list = ["Hello""World"]
List[0].uppercasestring//hello
The scope of use of generics is not limited to parameters, and we can also define generic classes, enumerations, and structs. In fact, in the previous example, the type of list is array<string>.
You might compare generics in Swift with the protocols in Objective-c, although their syntax is very similar, but the concept is very different. OBJECTIVE-C does not support generics. The protocol provides a way to declare that a certain implementation conforms to a message contract, but the contract must be specified beforehand. For example, using a protocol you cannot force all entries in an array to be of the same type (regardless of type), but can be done using generics. In addition to the conceptual differences, Swift does not support the protocol, just as OBJECTIVE-C does not support generics.
Meta-group
Tuples are a very simple concept, and you can define an ordered set of values. Tuples are useful when you need to pass multiple values back and forth as a parameter, or if the called function needs to return multiple values. Tuples do not require that we define any type for its value, and all type inference and type checking work is done at compile time. The syntax for defining tuples is as follows:
"Both", ["three"])
In the example above, we created a tuple with three values, the first one is an integer, the second is a string, and the third is an array of string types. At first glance this is much like an array, but the concept is completely different. You cannot remove or append elements from a tuple, and notice how the compiler knows the exact type of each value:
You can refer to its value by its position within the tuple, as shown by the positive element, or you can specify a name for each value. This is handy if a function needs to return several values, but it also allows us to avoid defining classes or structs that are specific to a function. Let's look at an example like this:
Func info (Items:[int]), (Avg:int, Min:int, Max:int) {
var sum = items.reduce (0, {$ + $})
var min = items.reduce (Int.max, {$ > $: $})
var max = Items.reduce (int.min, {$ < $: $})
return (Sum/items.count, Min, max)
}
var result = info ([1, 2, 3, 4, 5, 6])
3
1
Result.max//6
Tuples provide a very simple way to use multiple values, eliminating the extra work of defining a particular class or structure.
There are more ...
In addition to the features described above, Swift has a number of other outstanding features that are worth looking at, such as property observers, optional links, and extensions.
I believe Swift has all the necessary conditions to quickly become a popular IOS and OS X programming language, both in the enterprise and in the consumer domain. The strong type and type inference feature will make it ideal for enterprise development, and its simplicity and clean syntax will appeal to developers who work on consumer projects.
Swift programming Language "Go + Finish"