Swift’s icon is like a swallow which is flying! Follow me, learn swift one by one, I believe we can fly!
(sort out it by internet,any similarity is purely coincidental)
Someday, I might add translations(😀😀😀😀😀).
Begin One : swift basics
We just created a variable, called it name and gave it a value. Variables are created with the keyword 'var', followed by a name and a value, which is assigned with '='
var task = "learn java"
task = "learn swift"
print(task) ==> learn swift
Just like I cant't handle more than one of these tasks at once, variables can only remember one value at a time.
If you want to give your variable a different value, simply use its name (without 'var')
Programs are written in code. That's another word for lists of instructions. With code, we can build great apps, games and websites. Sit tight, Let's look at the following:
var love = "I love"
var cookies = "peanut biscuit"
print(love + cookie + "!") ==> I love peanut biscuit!
The word print followed by '( )' reveals the output, specified by the string inside the brackets.
Psst:print( ) is called a function
Variables can store lots of different things. We've already seen that we can store strings. But wait : numbers can be stored as well!
var myString = "4"
var myNumber = 4
We can see: myString holds a string and myNumber has the value 4.
It's important to notice that there's a difference. If you want a variable to hold a meaningful value, just use a number without "quotation marks".
Numbers come in different types. Integers can hold whole numbers, Doubles can hold numbers with a decimal place.
If you assign a number to a variable, the variable automatically knows which type of number it can hold.
var myInteger = 2
var myDouble = 2.0
myInteger = 10(正确)
myInteger = "10"(X)
myInteger = 10.0(X)
The variable myInteger has the value 2 assigned to it. From now on, it will only take Integer values. "10"(string) and 10.0(Double) won't work.
With code, we can also do math!
var five = 5
var six = 6
var result = five + six
print(result) ==> 11
We can do basic math operations with '+', '-', '*', '/'. Our beloved print( ) function doesn't just work with strings but with numbers as well!
We can change the values of variables at any time. If we know that the value of a variable won't change, it's a good idea ti use let instead
My name is 'dabaodeaiqing' and I sure hope that'll stay this way. so:
let name = "dabaodeaiqing"
then my name is a constant and can't be tinkered with!
If we don't want to assign a value to a variable upon creation, the variable doesn't know what type of data it can hold. In that case, we need to give the variable a type.
var myString: String
var myInteger: Int
var myDouble: Double
We've created three variables that can hold three different types of data. Stay tuned, we''ll talk about many more types in Swift!
We've already learned a lot about programming. We know how variables are created, what the difference between var and let is, how to combine strings and how to peint something out.
var numberVar = 42
var stringVar: String
let pt1 = "Let's learn swift"
let pt2 = "happily!"
var output = pt1 + pt2
print(output) ==> Let's learn swift happily!
Thanks a lot!
网友评论