Студопедия
Случайная страница | ТОМ-1 | ТОМ-2 | ТОМ-3
АрхитектураБиологияГеографияДругоеИностранные языки
ИнформатикаИсторияКультураЛитератураМатематика
МедицинаМеханикаОбразованиеОхрана трудаПедагогика
ПолитикаПравоПрограммированиеПсихологияРелигия
СоциологияСпортСтроительствоФизикаФилософия
ФинансыХимияЭкологияЭкономикаЭлектроника

Writing Swift in playgrounds

Course details | Course details | Welcome | Welcome добро пожаловать | What you should know | What you should know Что вы должны знать | The structure of Swift | Declaring variables | Declaring variables Объявление переменных | Creating constants |


Читайте также:
  1. Creating loops in Swift
  2. Creating loops in Swift Создание петель в Swift
  3. English phonemes in writing
  4. II. Writing
  5. The structure of Swift
  6. The structure of Swift Структура Swift
  7. Understanding access modifiers in Swift

 

- As a programmer, not just in the Apple world, but in Windows or Linux development too. If you have an idea and you just want to write a few lines of code, well you often can't. You can't write a few lines of code. You have to create some structure for it first. That often means creating a new project in your IDE whether that's Xcode or something like a clip or visual studio. Another, create a folder somewhere, perhaps with some configuration files and spawn up some default classes and boilerplate code. Maybe a user interface. All just to allow you to finally write your few lines.

Most developers end up with dozens of tests something, something projects all over their systems. Okay, not a huge blow back but another speed bump. With Swift, we don't need to do that. We can use something called playgrounds. Playground is a new feature in Xcode six introduced for the Swift language. There are new document type. A Swift playground is a self-contained single file, not a project. That's kind of the point. It lets you quickly write some code and test that code without all the overhead and wait what becomes with the project.

But it's not just about having a quick way to write code. What's unusual and different about playgrounds is it continuously show you the results of your Swift as you are writing and changing it. They automatically execute and refresh all the time. They're great way to experiment with Swift and to learn it. In this course, we'll use playgrounds rather than creating entire projects in Xcode. When you open Xcode six, the first option on the welcome screen is to get started with the playground. Although you can use this option, you can also create a playground from the regular file menu in Xcode.

Either way that you choose to do this, I'm going to ask you if you want this playground targeted at iOS or OS X development. All this is going to do is link to the typical frameworks you'd need if you're writing either iOS or Mac applications. Now for us in this course, it doesn't matter at all. We're focusing on the Swift language itself, not on user interface stuff. So I could choose either. I'll just leave it at iOS and click next. Then we'll just save this somewhere, anywhere. It doesn't matter. There's no magic location for playgrounds. I'll save mine to the desktop.

The font is a little small so I'm going to make one quick change to Xcode. You don't need to do this but I just want to increase the size of it so it makes it easier recording this course. A new playground by default appears with two sections. A code editor section on the left and the results pane on the right. You can drag the bar in the middle to rearrange the proportion of this. There is a third section to playgrounds called the timeline which we'll see in a moment. But what you won't see when a playground opens is the usual project settings, project editor, configuration options, toolbar, etc, etc, etc, that you would usually get in a typical programming IDE because, although Xcode can certainly do all that, again, this is simple.

This isn't part of a project. This isn't a part of anything. It's just a standalone Swift playground file. We write our Swift code and it's a little here provided by default. As we write it, we'll immediate see the impact of that code appear in the results pane on the right hand side. That's without explicitly running it or compiling it. So I'm going to leave the default code here and just create a simple variable. Var highscore equals 100. It's going to show me immediately the result of this expression is that high score is 100.

I then decide to add something to this. Add 50. It's going to recalculate that. I'm not hitting any keys here. It just shows me that it's 150. As I'm typing, you'll see the text in the results pane will flicker between grey and black. That's because as we add and change code, and if I change this, say to 99, it's going to update and recalculate and reevaluate itself to make sure the results are all still correct. So when the results are grey, what's happening is the playground is recalculating. When it's finished recalculating, the results are shown in black text.

If you make it change and the results on the right hand side are still in grey, then there's something wrong with your code and the playground can't figure it out. Now not all code you write will show you individual single values on the right hand side. Let's say I add a simple loop. Again, don't worry about the exact syntax yet. We'll get to that. This loop is going to iterate a hundred times, incrementing an index called i and adding the current value of that to our high score variable. So the results pane just shows me here that this happens 100 times.

Well, this is pretty good. At a glance, I can tell if I got an off-by-one or a fencepost error but it's not showing me the actual value of this high score variable and how that is being changed inside the loop. But if I mouse over this line in the results pane section, I'll see two icons appear. This one's called the quick look icon, the eyeball. We'll get into this later. There's also a circle with a crosshair called the value history button. If I click the value history button, I will see the third section of the playground appear called the timeline.

We just rearrange this a little bit so it's easier to read. The timeline is going to generate a chart which shows the changes over time that come from this statement. The addition to the high score variable inside the loop. Well not only this but there's a slider at the bottom of the window and I can drag this to simulate going back and forth through the iteration of this loop and seem how the value changes for everyone. This can be extremely useful when you're diagnosing loops that aren't doing what you'd think they should be doing. Now having said that, I don't always need the timeline.

If I simply wanted to know the final value of this high score variable after the loop has finished executing, I can easily get to that too. I'll close the timeline down. Just go back to the regular view here. Now to write a line of a Swift to spit out the value of a variable, you might expect that we need some kind of print statement or a console statement or log statement. But in a playground, we don't even need that. The simplest way to see the value of a variable here is just to write the name of this variable on its own line.

I'll just write high score. Because as far as the playground is concerned, this is just a very simple expression and it's going to evaluate that expression and you'll see the results over here in the right hand side. The current value of high score. So notice, we're not just seeing the final result. We are seeing the values change over time without writing any logging statements, without even explicitly executing this code, and, as a playground, is constantly updated. I can make a simple change. They, back up at the top. Change this one to 999 and it will immediately trickle down and show the resulting effects on the rest of the code.

So they're a fantastic interactive way to learn Swift. Even after you're comfortable with it, they're great way to experiment and develop ideas. You can always take code that you've written in a playground and put it into a more formal project later if it's useful. All right, there is more to playgrounds but this is certainly enough to get us moving forward into the core syntax of Swift.

 

 


Дата добавления: 2015-08-20; просмотров: 67 | Нарушение авторских прав


<== предыдущая страница | следующая страница ==>
The structure of Swift Структура Swift| Writing Swift in playgrounds Написание Свифт на игровых площадках

mybiblioteka.su - 2015-2024 год. (0.007 сек.)