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

The structure of Swift

Course details | Course details | Welcome | Welcome добро пожаловать | What you should know | Writing Swift in playgrounds | Writing Swift in playgrounds Написание Свифт на игровых площадках | Declaring variables | Declaring variables Объявление переменных | Creating constants |


Читайте также:
  1. A dynamical model of a crystal structure
  2. According to the internal structure, word combinations with head element are
  3. Concrete and reinforced-concrete structures
  4. Creating loops in Swift
  5. Creating loops in Swift Создание петель в Swift
  6. Database structure
  7. Deviation tolerances of building structures

- Before we jump into Xcode let's take a look at some basic Swift code. Don't worry about the exact syntax right now, I just want to point out a few things about the general appearance and structure of Swift. Now if this was the first Swift code you had ever seen, as a programmer you could still make a few assumptions about this within a few seconds. That first it seems to be a C-influenced language. By that I mean we're using curly braces to surround blocks of code, there are keywords in lowercase, like if, else, for and return. This if statement should be fairly readable no matter what programming language you're used to.

So yes, Swift is absolutely a C-influenced lanague. We use curly braces, there are many keywords you'd recognize from working in any other C-influenced language, although as we'll see there are also the keywords you'd expect to find in an object-oriented language like class, private and public. But there are keywords you might not be familiar with perhaps like func here, although you could make a good guess at this and there are other keywords that are specific to Swift. And we will of course go through all of these, but while Swift is C-influenced it is not shackled or handcuffed to C.

Unlike the Objective-C language, Swift does not attempt to be C compatible. As Objective-C developers will know, you can take a section of old-school C code from 1970-something and paste it right into an Objective-C program. Now you try that in Swift and all you will get is a screen full of compile errors. Swift is its own language, it's not a superset of something else. So what other similarities do we have? Well we do indent our code in Swift as you can see, but indentation is just for readability. Unlike say Python, indentation in Swift is not meaningful in itself though there are some things I'll point out about whitespace shortly.

Now comments are as you might expect in a C-influenced language. It's just // to begin a single line comment and multiline comments would begin with /* and end with */, but while there are many similarities Swift also immediately gives us some key differences from most C-influenced languages. First, Swift statements do not need semicolons at the end. Notice how I'm saying this, you don't need semicolons. You can have 'em, it won't cause an error if you put a semicolon at the end of a statement.

And there's two reason why you might have them. First is if you want to write multiple statements on one line, you would separate them with a semicolon. This is not a very useful reason, but you could do it. Second, the more likely reason is simply that you, like me, have a habit of years of typing semicolons at the end of your statements so if you do unconsciously write one you don't want it to cause a compile error every time. So they're allowed, they're not needed. Sidebar, if you program JavaScript you'll know it's a language where you can omit most semicolons, but even though you can most programmers make it a habit to write them anyway.

That's what the style guidelines will recommend for various reason including some pesky bugs, so know that in Swift this does not apply. The standard practice is you do not write semicolons. Apple's own sample code does not use them, end sidebar. The next difference is that unlike many C-style languages we do not need a main method or a main function to say where a basic Swift program begins. If I just wrote some Swift like this and ran it, it would start at the top and work its way down. And sure as we get into more complex applications spread across multiple code files we will want to control the entry point and we can certainly do that, but we don't need a main method for simple Swift code.

And that leads us into the next question, well how do we write and run a simple Swift program? Up next.

 

 


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


<== предыдущая страница | следующая страница ==>
What you should know Что вы должны знать| The structure of Swift Структура Swift

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