![]() For example, you want to have different outputs depending on the result of a calculation. For this reason there are so called conditionals that control the program flow. Of course there are more types: var aString: String = "A String"Ī program that does always the same is very boring. It is very clear at the first sight that the variable has the type Int. The result is the same as in the previous example, but now the code is easier to read. But it is also possible to specify the type directly: let aNumber: Int = 10 Type AnnotationsĪs we have learned already, variables gets a type after the first declaration. ![]() For example, if you assign the name of a person, the value should not be changed again. So why should you even use a constant? Well, sometimes you don’t want that a value gets changed accidentally after its first declaration. If you now try to change the constant, there will be an error: aConstantNumber = 10 // Compiler Error For that you have to use the keyword let: let aConstantNumber = 10 But there are also constants, that cannot be changed after their first declaration. ConstantsĪs the name says, a variable can be changed. If you are doing something that the compiler doesn’t understand or is against the rules, there will be an error. The compiler is a program that translates your code into machine code. However, after you have assigned one value, you can only assign the same type: aBool = "A String" //Compiler Error So you can also assign for example a string, floating point number or a bool (true or false): var aString = "A String" For that you need to put the value in a special term: print("The number is equal \(aNumber)") ![]() It is also possible to print the value of a variable. We can even assign a mathematical term and the result gets stored in that value: aNumber = 5 + 6 What can we do now with it? Well, first of all we can change the value: aNumber = 10 Now we have a variable called aNumber that has a value of 5. For that, we use the keyword var: var aNumber = 5 Variables enables you to store a value like a number or a string in a placeholder. On the right side of the playground you can see now the output.Ĭongratulations, you have just programmed your very first Swift program! VariablesĪfter printing the first sentence, we want to do some real programming. So take your playground and enter the following line: print("Hello World!") In most of the programming books the very first program is one that simply prints “Hello World!” And we don’t want do break with good old traditions, do we? Afterwards, you have to specify the name and the location of the playground and we can start. If you don’t see that window, click on File -> New -> Playground. Then click on “Get started with a playground” in the welcome window. Also for this Swift tutorial you should practice everything in a playground. This way you can quickly perform some small experiments. If you are working on a project, it is very advisable to always have a playground open. So if you want to learn something or if you want to experiment a little bit, you can just open a playground and do it without compiling and executing a huge project. They allow you to write Swift code that gets directly executed. But Xcode has one very interesting feature that is a huge help for both beginners and experts: Playgrounds. Most of the times you will work in a project. Xcode is Apple’s IDE for developing for all of the Apple platforms: iOS, watchOS, tvOS and Mac OS. You can install it directly from the App Store. The first step in becoming a Swift guru is downloading Xcode. There will be more parts in this “Swift Tutorial For Beginners” series. This Swift tutorial includes the following topics: What will you learn in this “Swift Tutorial For Beginners”? In this Swift tutorial you will learn all its basics. Swift is Apple’s new programming language for developing applications for iOS, watchOS, tvOS and Mac OS.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |