Swift programming. Swift Programming Courses


Swift is an incredibly powerful and simple programming language introduced by Apple in 2014, which has given many programmers the opportunity to create applications for iOS, MacOS, Apple TV and Apple Watch. This language inherited ideas from Objective-C, Rust, Haskell, Ruby, Python, C# and CLU. It uses secure programming patterns and contains new features to make programming fun and interesting. Created by Chris Lattner, it is reliable, fast and open language, making it even easier to create apps for the world's most popular phone.

Swift is a modern programming language with a stable release version that is quite easy to learn, even if you consider that it is your first language. It prevents a lot of errors at the compilation stage, speeds up the development process and improves code quality.

Swift philosophy

The main philosophy of the language is its simplicity, speed and security. The entry threshold has changed to big side, compared to the same Objective C - even without knowing any other programming languages ​​or basics, you can safely start learning the language.

Apple has simplified the language for developers as much as possible: the syntax has become cleaner, which increases the readability of the code for both experienced and novice programmers. The total amount of code has decreased, resulting in increased productivity. All methods that were used in Objective C were completely redesigned, rethought and rewritten. Swift is constantly being developed and updated, which indicates its reliability and focus on the future.

Where is Swift used?

Developers use this programming language to write mobile applications for phones, smart watch and Apple computers. There was even a rumor that Google wants to transfer Android completely to Swift, anything can happen. This is a universal language that can be used in any platform from the Cupertino company.

The important point is that in the fall of 2016 Apple of the Year contributed to the opening of a student training center where anyone can learn how to program iOS applications. But that's not all, Swift is already included in the states curriculum many leading educational institutions. This programming language has huge potential.

Swift Learning Difficulty

Swift is a safe language that will protect you from most mistakes in the programming process. It will tell you where the error is and why the program does not compile. But on the other hand, there is strict typing here. Apple's language is unforgiving.

On this moment There is little documentation available compared to other languages. But this can be due to the fact that the language recently saw the light and became Open Source project, because at the time of release it could only be used by those developers who had an active Apple subscription Developer Program.

Even earlier it could be said that Swift is not very popular and there are a small number of third-party libraries and extensions for it, but now this is completely not the case. There are dozens of them on GitHub and the Internet. excellent solutions from third-party developers who will help improve your project.

Pros/Cons of Swift

This modern and powerful programming language has a number of advantages that set it apart from others or the same Objective-C. And these are new powerful language features, extremely strict data typing, more concise syntax, significantly higher performance in collection access operations (sorting and searching), support for a certain majority of modern syntactic constructs existing in other high-level programming languages. And one of the main features of Swift is that it has complete backwards compatible with Objective-C. Thus, this allows you to gradually rewrite projects from one language to another, and also makes it possible to use Objective-C functionality within Swift itself.

One of the nice things is the Playground - this is a kind of interactive sandbox where a developer can write code and immediately see the result of his work. This allows you to increase the speed of writing the main thing several times, because writing individual pieces of code or algorithms can now be carried out in the playground with a visual feedback, without the risk of making mistakes in the main project.

Apple has reworked a lot of things, giving us a virtually new advanced enumeration system, efficient accessor management, and optimized automatic garbage collection.

The dark side of the language is its low popularity and low prevalence. Most current projects are written in Objective-C and will have to be supported, and many companies do not rewrite their big projects only because it is a huge and incredibly time-consuming process.

Related Technologies

Apple has made the transition from using static libraries to dynamic and added this functionality to its programming language, which means its faster development than iOS itself. Now developers can easily present their application along with the libraries they have written. Previously, you had to wait for a new one iOS release, but now the situation has changed. Dynamic libraries provide the opportunity for more flexible improvements and code changes than ever before.

Swift is more functional and accessible language, which, like Objective-C, supports the official Cocoa and Cocoa Touch frameworks. There are many available online open projects, including from such large companies as Airbnb, which allow you to use everything modern methods and decisions when writing code.

As has already been noted several times, including by , the conference was excellent, even if no hardware was presented at it. The head alone was enough software products. How much the public liked the conference is evidenced by the fact that after it, nothing noticeable, traditional for such cases, happened.

And while much of what was presented was quite expected by the public, the programming language turned out to be a real surprise. After all, no one even suspected that Apple was working in this direction. However, the programming language is not the iPad; developments of this type are of little interest to the general public. This means that maintaining secrecy was much easier.

The most secret project

In fact, work on Swift began back in 2010, after iPad presentations. The company wanted to create a simple, convenient and functional tool that could make life as easy as possible for developers.

The work was carried out in the strictest confidence. Moreover, for quite a long time only one person worked on the project - Chris Lattner, director of the Developer Tools department. Of course, the staff subsequently had to be expanded. But still, the number of these people has never been large enough. Few people knew about the development of a new language, even within the walls of Apple itself, although by 2013 it was considered the most important Developer Tools project. It is important to note that the company wanted to present the finished product right away, avoiding all kinds of beta versions. Therefore, the work stretched until 2014.

Here it would be appropriate to talk a little about Lattner himself. He came to Apple in 2005, before that he worked for the benefit of the LLVM project at the University of Illinois, being one of the leading developers.

LLVM (Low Level Virtual Machine) - low-level virtual machine, universal system transformation, optimization and analysis of programs. The project began as an independent project, but subsequently received significant outside support. Including from Apple itself, which then actively used LLVM and own projects, including OS X and iOS. The same Metal was developed using this system.

LLVM could not be avoided in the case of Swift either. Using this system, the language is compiled into machine code, optimized in such a way as to obtain maximum return, when using the application on Apple equipment.

At one time, Chris Lattner's transition to Apple was more than logical step. No less logical was the decision to entrust the development of a new language to him. It's too early to make a final verdict, but it looks like he did a great job.

Features of Swift

The brainchild of the latest developments, the compiled object-oriented programming language Swift builds on languages ​​such as Objective-C and C, bringing together the best of them. There are no restrictions in terms of compatibility; languages ​​can coexist perfectly in one application. The latter, by the way, is very important, since there are already a huge number of programs written in Objective-C.

Interestingly, the vague idea of ​​Swift appeared back in the days of NeXT, when Objective-C had just begun to be developed. Then the idea was born of creating a language that could make the programming process simpler and more fun. But, as we see, it took a lot of time to implement this idea.

In fact, it is not so easy to find that line when the code is simple enough to type, but at the same time readable enough for a third-party developer to understand pieces of someone else’s program without commentary. For example, a program written in Pascal is easy to understand, but the code is too long. PERL allows you to write everything in a few lines, but is characterized by extremely unreadable code.

Meanwhile, some developers who have already tried Swift in practice claim that Apple managed to come as close as possible to the ideal, to find that golden mean between readability and ease of typing.

As is immediately clear from the name of the new language ( "swift" - English. "fast, swift"), the main emphasis during its creation was on speed. In particular, it is stated that a program in Swift will run 1.3 times faster compared to a similar one written in Objective-C.

Also, when developing Swift, the following were taken into account: important parameters like simplicity, security and flexibility. This language eliminates entire classes of unsafe code and is capable of automatically managing memory. Swift eliminates the very possibility of making a number of mistakes that are so common among novice programmers. As for simplicity, it is well confirmed by the fact that within 24 hours from the moment, it is already on it.

The creators have put a lot of effort into making Swift code more expressive and easier to understand. The developer can immediately track the results of his creativity by viewing individual parts of the code in the so-called. “interactive playgrounds” (Interactive Playgrounds). If it takes quite a lot of time to execute the code, then you can monitor the process on a special feedback line that appears for this purpose. After the code has been improved and the algorithm has been perfected, the developer simply moves it to his project. Playgrounds can be useful not only in terms of simplifying the development process, but also in sharpening your Swift skills.

A traditional "Hello, World" application looks like this in Swift:

println("Hello, world")

And here is another example of simple code:

let apples = 3
let oranges = 5
let appleSummary = "I have \(apples) apples."
let fruitSummary = "I have \(apples + oranges) pieces of fruit."

Criticism of Swift

Meanwhile, openly negative opinions regarding Swift are also appearing on the Internet. There are concerns that this is a dead-end development path and Swift will never be able to become popular. After all, even before this, there were many programming languages ​​that were created precisely with the goal of making the lives of programmers easier and simplifying the learning process. And not one of them fully coped with the tasks assigned to him. All that their creators managed to achieve was simply to increase the already considerable number of programming languages.

There is criticism of Apple's actions, which "would be better off developing languages ​​like Python or Ruby, instead of creating another one." Such words are supported by the opinion that these tools are already actively used by developers from all over the world. I also happened to find an opinion according to which Apple is in vain making its main bet on speed. Like, when modern levels iron development is not that important.

But what causes the most irritation is the fact (as always in such cases) that Apple created something only for itself, according to its own standards and on its own principles. Moreover, it is stated that although Swift is not very different from many other languages, the company again declares the creation of a fundamentally new product.

Instead of an afterword

One can criticize Apple for being overly closed and arrogant. You can admire the simplicity of Swift and the cleanliness of its code. But the fact remains that the future of the new programming language is still unclear. And two development scenarios are possible.

Scenario one. Swift remains just an interesting development. It is used, but not too actively, preferring to continue working with the proven and familiar Objective-C. Developers for other platforms do not notice the emergence of a new language at all.

Scenario two. The approaches used in Swift are becoming the norm in the programming world. Apple is once again managing to influence the entire world by changing the norms generally accepted among developers. The idea of ​​"playgrounds", coupled with the transparency of the code of the new language, may well revolutionize the learning of programming. And this will allow us to completely reconsider the entire educational process and provoke the emergence of a new generation of programmers.

By the way, I wouldn’t be surprised if in a few years, having thoroughly “polished” Swift, bringing it to perfection, Apple declares that it intends to abandon Objective-C altogether and rely on new language.

I use technology Apple yet since the “classical era”, since 1995. During this period, I saw more than one sudden and significant (and sometimes downright shocking) “revolution” in the development of the company. Which, despite everything, did not lose its charm. I believe that it will continue to be so.

Chris Lattner spent a year and a half creating a new programming language. During this time, he did not talk about his work to anyone, even among close friends. He started working in the summer of 2010, devoting nights and weekends to the task, and by the end of the following year he had developed the basics of the future language. Only then did Chris reveal his secret within the company, whose top managers were impressed enough to add several experienced engineers to the project. Another year and a half later, Lattner’s project was included in the list of the company’s main directions, and the development team expanded significantly. Today, a new language is on its way to change the world of computing. The company Chris Lattner works for is called Apple, and the language he created is called Swift.

On June 2, 2014, at the WWDC presentation, the Swift language. The company released a test version for third-party developers and programmers, positioning the language as faster and effective method creating programs for iPhone, iPad and Mac. Even after Chris Lattner began working on Swift, information about the product shocked everyone except a limited number of Apple employees. Even people who were indirectly involved in the creation of the language and helped Chris were very surprised to find out this summer what exactly he was working on.

Swift Captures Audiences

It usually takes a new language several years after its introduction to gain any audience. This rule applies even to large companies like Apple. A good example is Google company with the Go language introduced back in 2009. The brightest minds in the software development world, Ken Thompson and Rob Pike, worked on Go, but to this day considerable efforts are made to ensure that the language has followers. But Swift is a different breed of beast. Once the language is officially released this fall, it is expected to gain an audience at an unprecedented rate, surpassing the adoption rate of languages ​​like Java and C# in 1990 and early 2000s.

How can such optimistic assumptions be explained? In fact, Swift is designed for the average programmer. You can write even the most simple applications, and loaded with some pretty smart tools, the language offers an effective way to learn to write on your own. But main reason The future popularity of the language lies elsewhere. Hundreds of thousands of developers today create applications for Apple devices using Objective-C. Apple devices are very popular, which means these developers will continue to create applications for them, but in Swift, since it is much better and more efficient than Objective-C.

"There was no real incentive to use Google Go," says Paul Jansen, who has been tracking progress various languages programming for about fifteen years. “What makes Swift different is the incentive.”

Today, GitHub, a popular open source development repository, has over 2,400 projects using Swift. It is worth remembering that at the moment the language is only available limited number programmers.

Advantages over Objective-C

To abandon a language in which several products have already been written and start learning a new one, programmers need really good reasons. Relearning takes time and effort, and to date Chris Lattner is the only person with four years of Swift programming experience. However, these very compelling reasons have already been presented.

It's not that Swift is significantly more attractive than other languages ​​such as C#, Ruby or Python, Swift is significantly more attractive than Objective-C, which is difficult to name modern language. Swift syntax is more familiar. The language also contains several tools for protecting against errors and bugs. Swift better understands the actions of the coder and makes his work more efficient, allowing him to do more in less time. But the most interesting aspect of the language is the so-called Playgrounds.

Playgrounds

Playgrounds are designed to make programming more interactive and accessible. The idea is that when a programmer writes code on one half of the screen, he sees the result on the other half. Thus, you can observe the creation of the program in real time. When a programmer makes changes to the code, they are also made to running version programs, eliminating the need for repeated compilations and restarts.

This approach to writing code will not only make a programmer's job easier, it will change the way we perceive programming, elevating it to a completely different level. new level understanding. Chris Lattner also hopes that by making coding easier and more fun, he will help redefine the way computer science is taught today.

More than a language

Chris Lattner, the creator of Swift, oversees all of Apple's developer tools. Everything that is used to create programs for phones, tablets and Apple computers, How third party developers, and the company's engineers. As a graduate student at the University of Illinois, he created a kind of developer tool called LLVM, which is the basis of Xcode today.

Apple used LLVM to change the way Objective-C was used to create applications. This happened after Lattner joined Apple in 2005, and five years later he used LLVM as the basis for Swift. These two products have been specifically designed to work in tandem. In other words, Swift is not just a language, it is a language that is closely related to everything a developer needs to do their job. This includes not only the integrated development environment, but also many other tools that every Apple developer is already familiar with, such as the debugger. In other words, there are reasons why tens of thousands of programmers should already be writing Swift apps.

Speed

This is exactly the word that best characterizes the various qualities of the new Swift language. The language demonstrates amazing speed of converting raw code into executable application. The speed of these applications on phones and tablets should be surprising. Swift is not only human-friendly, but also computer-friendly. And the third quality, which we have already talked about, is the speed of spread.

Apple is not the first company to enter this ring. It already has , Google and Mozilla. In some ways, the languages ​​of these companies have gone much further, since they are already used for internal work. Moreover, most of them open source, making them easier to distribute to other companies. Apple is known for its tight control over its own products, and at the moment source is closed, which can, for example, make it difficult to create cross-platform programs. However, it is believed that Swift will eventually be open sourced.

Regardless of whether Swift is open source or not, it is predicted to have unprecedented popularity and adoption rates. The uniqueness of the language, as well as the popularity of Apple products, will undoubtedly do their job.

Based on Wired

So, let's continue our acquaintance with the simple and interesting language Swift. Last time we discussed a promising programming language. And now Let's make our first application!

How to create an application for iOS

Today we'll make it simple application for calculating body mass index and the required number of calories to maintain weight. To do this, we will use the formulas of Garissa Benedict and body mass index.

1. First, let's create a project: launch Xcode and press CMD+Shift+N or select from the menu File->New->Project

2. Now select Tabbed Application and press Next.

3. At this stage, you need to come up with a name for the application (Product Name), click Next and select a folder to save. Options Organization Name And Organization Identifier you don't have to change it.

The main thing is that the value in the Language field is equal to Swift.

4. After you save the project, the Xcode workspace will appear in front of you.

On the left you will have the Navigator panel and on the right you will have the Utilities panel. Each of these panels has its own tabs that allow you to access different functions.

For example, the first tab in the Navigator panel shows a list of project files, the third allows you to search by project, and so on.

5. In the list of project files, find a file named Main.storyboard and click on it.

The Main.storyboad file determines which screens (controllers) is in the application. You can add elements to screens, create connections between screens, and so on.

If you select a controller, it will be highlighted with a blue frame:

Once the controller is selected, its properties will begin to appear in the panel Utilities. For example, a controller can be resized by selecting a different value in the field Size.

6. Let's run our template application and see how it looks in the simulator. To do this, select the type of device being simulated in the upper left corner and press the key combination CMD+R or the icon button Play.

As you can see, we have an application with two screens, switching between which is done using the tab bar.

7. The name of the tabs needs to be renamed. In order to do this, you need to select the tab by clicking on it or selecting it in the hierarchy of controls.


Once the tab is selected, it will be highlighted in green.

8. Now in the Utilities panel select Attributes Inspector and change the value in the Title field to BMR/BMI for the first tab and Kilocalories for the second one.

Now on the first controller there are all sorts of inscriptions. They need to be removed. To do this, select them and click Delete.

9. Now let's add our controls. At the bottom of the Utilities panel there is Object Library, in which you can select objects and add them to the scene. Find an object Label and drag it onto the BMR/BMI controller. Before doing this, do not forget to double-tap on the stage so that the scale becomes standard.

It should look something like this.

In the Utilities panel you can change the text, font size, and so on.

10. Drag a few more labels and give them the same text as in the image. For the last label you need to put Lines V 2 so that the text wraps to another line.

11. Now add 3 text fields ( Text Field).

And place them as in the picture.

For text fields, set the parameter keyboard type V Number Pad.

And set the default text.

12. Now let's add controls for selecting gender and number of workouts per week. Looking in the Object Library Segmented Control and add it to the screen.

With Segmented Control, you can change the number of segments and the text for each segment.

Make the segments look like the screenshot.

13. Now add a button ( button).

And set a title for it.



After that add another label with lines equal 4 .

14 . Select the BMI/BMR controller, click the two intersecting rings on the right top corner and it will open Assistant Editor. It shows the code associated with this controller.

15. Let's drag the controls through.

To do this, select the first one text field(in our case this will be age), press Ctrl, click on it again and, without releasing the button, drag it inside the class. If everything is done correctly, you will see a story “Insert Outlet, Action, …”.

Now release the cursor and you will see a window for creating a connection. For name, enter ageTextField and press Connect.

16. You will have a variable ageTextField.

Do this procedure for the remaining text field, segmented controls and label with a text result. You should get something like this:

Now drag the button, but specify the connection type not Outlet, but Action. And as the name use calculateTapped.

17. Now copy the following into the method calculateTapped.

This code performs the calculation and displays the result on the screen.

18. It's time to sum it up. Let's run it and check:

Your first program is ready! Well done!

Ask questions

If something doesn’t work out along the way, write in the comments. We numbered each item specifically for this purpose. All questions will be answered by professional developers from the team, so don’t be shy.

I worked on the text Ruslan Gumenny- lead developer e-Legion. The company is a leader in the customized market mobile development in Europe, part of the DZ Systems holding. Over 9 years of existence, applications have been created for RaiffeisenBank, Yandex, BMW, Bank of Moscow, Channel One, Aviasales, Sports.ru, VGTRK, Mail.Ru Group and many other companies.

If you are researching the topic and have something to add, do not hesitate to write to the editor with your articles on the topic of application development. In the next issue we will take a closer look at what has been done and finalize the application, preparing it for publication. So prepare yourself in advance by doing your homework :)

(No votes)

website So, let's continue our acquaintance with the simple and interesting Swift language. Last time we knew basic commands and functions of a promising programming language. Now let's make our first application! How to create an application for iOS Today we will make a simple application for calculating body mass index and the required number of calories to maintain weight. For this we will use the Gariss formulas...

At the opening of the WWDC 2015 developer conference, Apple presented updates to its key iOS platforms 9 and OS X El Capitan. But for developers, one of the most important announcements was the second version of the programming language. Craig Federighi said during the product presentation that Apple tried to create a language that would be free of the cumbersome legacy of Objective-C. Swift was built on the NeXt platform, which became the basis for OS X and then iOS.

Swift is really convenient and powerful language, which claims not only to replace Objective-C in the Apple ecosystem, but also to encroach on the entire C family as a whole. Blogger Anton Mukhataev, studying several programming languages, five Apple benefits Swift for beginners. These include easy-to-read syntax, clear constructs, security, and the lack of experienced Swift programmers.

1. There are still no truly experienced Swift developers

Everyone, without exception, has to learn and relearn. If you take up Swift in 2015, then after any number of years you will be at least a year behind the most experienced programmers. The only advantage for experienced coders is that Swift borrows the best elements of other languages ​​- those who know them have an easier time mastering it.

2. It's a young programming language

New versions of Swift continue to see fundamental changes to syntax and data types. The language is maturing before our eyes, and this makes it easier to understand the logic of change - and if you wish, you can help improve it. Of course, some may call this a disadvantage, given that projects written on older versions of Swift do not work on new ones - but the migration does not take that much time, and the syntax only becomes simpler.

3. It is a safe programming language

Swift is based on the desire to leave as few vulnerabilities as possible and make the code as secure as possible from errors. The programmer is not required to keep everything in his head and keep track of everything. This is necessary in C-like languages, and if earlier this was justified because they worked faster than others, then Swift is catching up in performance even with C++ - and the further it goes, the better it will be optimized. A programmer who writes code in Swift is deprived huge amount headaches related to memory management and other things.

4. Easy to read syntax inspired by Python and Ruby.

Swift - easy readable language, just like Python. The main difference is that blocks are separated not by indents, but curly braces- it’s more familiar and convenient.

5. Apple open source Swift

Apple announced at WWDC 2015 that Swift will be . This means not only that Apple will rely even more heavily on the developer community to refine it, but also that it has the potential to become truly multi-platform, rather than being limited to the Apple ecosystem. In other words, if you are developing an application in Swift for iOS, then there is a prospect that you will be able to port it to Android, and you will not need to learn the Java used operating system Google.

Finally this beautiful language. It’s very subjective, of course, but Swift code looks beautiful - which can’t be said about Objective-C with its endless clutter. And most importantly, beauty and convenience do not affect power.

A guide to the programming language in iTunes; information about the language is also available on the company’s website for developers.







2024 gtavrl.ru.