Σε αυτό το άρθρο, θα μάθετε για μεταβλητές, σταθερές, κυριολεκτικά και τις περιπτώσεις χρήσης τους στον προγραμματισμό Swift.
Τι είναι μια μεταβλητή;
Στον προγραμματισμό, οι μεταβλητές χρησιμοποιούνται για την αποθήκευση δεδομένων στη μνήμη που μπορούν να χρησιμοποιηθούν σε όλο το πρόγραμμα. Σε κάθε μεταβλητή πρέπει να δοθεί ένα μοναδικό όνομα που ονομάζεται αναγνωριστικό. Είναι χρήσιμο να σκεφτείτε μεταβλητές ως κοντέινερ που περιέχουν πληροφορίες που μπορούν να αλλάξουν αργότερα.
Μη τεχνικά, μπορείτε να σκεφτείτε μια μεταβλητή ως τσάντα για να αποθηκεύσετε ορισμένα βιβλία σε αυτήν και αυτά τα βιβλία μπορούν να αντικατασταθούν με άλλα βιβλία αργότερα.
Πώς να δηλώσετε μεταβλητές στο Swift;
Στο Swift, χρησιμοποιούμε τη λέξη-κλειδί var για να δηλώσουμε μια μεταβλητή.
Παράδειγμα:
var siteName: Συμβολοσειρά εκτύπωσης (siteName)
Έχουμε δηλώσει μια μεταβλητή που ονομάζεται siteName του τύπου String
, που σημαίνει ότι μπορεί να διατηρήσει μόνο τιμές συμβολοσειράς. Επισκεφτείτε το Swift Strings για να μάθετε περισσότερα σχετικά με τις χορδές.
Εάν προσπαθήσετε να εκτελέσετε τον παραπάνω κώδικα στην παιδική χαρά, θα μας δώσει σφάλμα χρόνου μεταγλώττισης (η μεταβλητή χρησιμοποιείται πριν αρχικοποιηθεί) επειδή δεν αποθηκεύει / περιέχει καμία τιμή.
Πώς να εκχωρήσετε τιμή σε μια μεταβλητή στο Swift;
Μπορείτε να αντιστοιχίσετε την τιμή σε μια μεταβλητή χρησιμοποιώντας τον τελεστή ανάθεσης (=).
Παράδειγμα 1: Δήλωση και εκχώρηση τιμής σε μια μεταβλητή
var siteName:String siteName = "Apple.com" print(siteName)
Ή
Μπορείτε επίσης να αντιστοιχίσετε την τιμή inline ως
var siteName:String = "Apple.com" print(siteName)
Όταν εκτελείτε το πρόγραμμα, η έξοδος θα είναι:
Apple.com
Το μεταβλητό siteName περιέχει τώρα την τιμή "Apple.com".
Δεδομένου ότι το Swift είναι μια γλώσσα τύπου συμπεράσματος, μπορεί να συμπεράνει αυτόματα (γνωρίζει) "Το Apple.com" είναι ένα String
και δηλώνει το siteName ως String
. Έτσι, μπορείτε ακόμη και να αφαιρέσετε τον τύπο (:String)
από τη δήλωση ως:
Παράδειγμα 2: Πληκτρολογήστε συμπεράσιμη μεταβλητή στο Swift
var siteName = "Apple.com" print(siteName)
Όταν εκτελείτε το πρόγραμμα, η έξοδος θα είναι:
Apple.com
Δεδομένου ότι, το siteName είναι μια μεταβλητή, μπορείτε επίσης να αλλάξετε την αξία της χρησιμοποιώντας απλά τον χειριστή ανάθεσης, αλλά χωρίς var
λέξη-κλειδί ως:
Παράδειγμα 3: Αλλαγή της τιμής μιας μεταβλητής
var siteName = "Apple.com" // Assigning a new value to siteName siteName = "Programiz.com" print(siteName)
Όταν εκτελείτε το πρόγραμμα, η έξοδος θα είναι:
Programiz.com
Τι είναι μια σταθερά;
Μια σταθερά είναι ένας ειδικός τύπος μεταβλητής της οποίας η τιμή δεν μπορεί να αλλάξει. Είναι χρήσιμο να θεωρείτε τις σταθερές ως δοχεία που περιέχουν πληροφορίες που δεν μπορούν να αλλάξουν αργότερα.
Μη τεχνικά, μπορείτε να θεωρήσετε σταθερή ως τσάντα για την αποθήκευση ορισμένων βιβλίων και αυτά τα βιβλία δεν μπορούν να αντικατασταθούν μόλις τοποθετηθούν μέσα στην τσάντα.
Πώς να δηλώσετε μια σταθερά στο Swift;
Στο Swift, χρησιμοποιούμε let
λέξη-κλειδί για να δηλώσουμε μια μεταβλητή.
Παράδειγμα:
let siteName:String print(siteName)
Έχουμε δηλώσει μια σταθερή ονομασία siteName του τύπου String
.
Εάν προσπαθήσετε να εκτελέσετε τον παραπάνω κώδικα, θα μας δώσει σφάλμα χρόνου μεταγλώττισης (σταθερά που χρησιμοποιείται πριν από την αρχικοποίηση), επειδή δεν περιέχει / διατηρεί καμία τιμή.
Πώς να αντιστοιχίσετε μια τιμή σε μια σταθερά στο Swift;
Μπορείτε να αντιστοιχίσετε την τιμή σε μια σταθερά ίδια με τη μεταβλητή χρησιμοποιώντας τον τελεστή ανάθεσης (=).
Παράδειγμα 4: Δήλωση και εκχώρηση τιμής σε μια σταθερά
let siteName:String siteName = "Apple.com" print(siteName)
Ή
Μπορείτε επίσης να αντιστοιχίσετε την τιμή inline ως
let siteName:String = "Apple.com"
Όταν εκτελείτε το πρόγραμμα, η έξοδος θα είναι:
Apple.com
Τώρα το σταθερό siteName περιέχει / διατηρεί την τιμή "Apple.com".
Όπως και οι μεταβλητές, μπορείτε να καταργήσετε τον τύπο ( :String
) από τη δήλωση ως:
Παράδειγμα 5: Πληκτρολογήστε σταθερή συντελεστή στο Swift
let siteName = "Apple.com" print(siteName)
Όταν εκτελείτε το πρόγραμμα, η έξοδος θα είναι:
Apple.com
But unlike variables, you cannot change the value of constants. So, you cannot do something as
Example 6: Changing the value of constants (Not allowed)
let siteName = "Apple.com" siteName = "Programiz.com" //compile time error print(siteName)
Above statement gives us an error because as we said the value of a constant cannot be changed once data is stored. This is the key difference between a variable and constant.
What is a Literal?
A literal is a value that appears directly in your source code. It can be a number, character, or a string etc. For e.g: "Hello, World" , 12, 23.0, "C" are simple example of literals. Literals are often used to initialize (assign values to) variables or constants.
For example:
let siteName = "Apple.com"
In the above expression siteName is a variable, and "Apple.com"
is a literal.
Types of literals in Swift
Integer literals
It represents a decimal, binary, octal, or hexadecimal value. It has four types.
- Binary Literals
- Represents binary value.
- Begins with
0b
.
- Octal Literals
- Represents octal value.
- Begins with
0o
.
- Hexadecimal Literals
- Represents hexadecimal value.
- Begins with
0x
.
- Decimal Literals
- Represents decimal value.
- Begins with nothing. Everything you declare in integer literal is of type decimal.
Example 7: How to use an integer literal in Swift?
let binaryNumber = 0b11111111 print(binaryNumber) print(1231)
When you run the program, the output will be:
255 1231
In the above program, there are two integer literals 0b11111111
(binary literal) and 1231
(decimal literal). The decimal value of 11111111
is 255, therefore the print(binaryNumber)
statement outputs 255 in the screen.
Similarly print(1231)
outputs decimal value 255 in the console.
String & Character literals
A string literal is a sequence of characters surrounded by double quotes and a character literal is a single character surrounded by double quotes.
Example 8: How to use string and character literal in Swift?
let someCharacter:Character = "C" let someString:String = "Swift is awesome"
In the above program "C"
is a character literal and "Swift is awesome"
is a string literal.
Floating point literals
It is used to initialize variables of data type float
and double
. It can be of two types:
Decimal:
It can have an optional exponent, indicated by an uppercase or lowercase e. For decimal numbers with an exponent of exp, the base number is multiplied by 10exp:
Example 9: How to use decimal literals in Swift?
let someFloat = 12.23 let someAnotherFloat = 3.14e2 print(someFloat) print(someAnotherFloat)
When you run the program, the output will be:
12.23 314.0
In the above program 12.23
and 3.14e2
are floating point literals. 3.14e2
is expressed with exponential and is equivalent to 3.14 * 102.
Hexadecimal:
Hexadecimal floats must have an exponent, indicated by an uppercase or lowercase p.For hexadecimal numbers with an exponent of exp, the base number is multiplied by 2exp:
Example 10: How to use hexadecimal literals in Swift?
let someFloat = 0xFp10 let someAnotherFloat = 0xFp-12 print(someFloat) print(someAnotherFloat)
When you run the program, the output will be:
15360.0 0.003662109375
In the above program 0xFp10
and 0xFp-12
are floating point literals. 0xFp10
is expressed with exponential and equivalent to 15*210 (F is represented as 15 in decimal). Therefore, print(someFloat)
outputs 15360.0 in the screen.
Likewise, 0xFp-12
is equivalent to 15 * 2-12. Therefore, print(someAnotherFloat)
outputs 0.003662109375 in the screen.
Boolean Literals
There are two boolean literals in swift. They are true
and false.
.
Example 11: How to use Boolean literals in Swift?
let result:Bool = false
In the above program, false
is a Boolean literal which is assigned to the constant result.
Rules and Best practices for variables and constants
- Choose a name that makes sense. For example, var name makes more sense than var n.
- Use camelCase notation to declare a variable or a constant. Camel-case notation starts with lowercase letter. For example:
var studentName let studentAge let address
- You can also define variables and constants without labeling it. Not labeling with name means you are not going to use it in the program. There are many cases where you want to create a unused variable. In that case you can use _ placeholder as:
var _ = "Apple.com"//string initialized but not stored in a variable let _ = "Apple.com"
Or even this is valid_ = "Apple.com"
- Use constants if you only need to set a value once and never need to change it again during a program. However, if you do need to change it at a later point, use variables.
- Τα σταθερά και μεταβλητά ονόματα δεν μπορούν να περιέχουν χαρακτήρες κενού διαστήματος, μαθηματικά σύμβολα, βέλη, σημεία ιδιωτικής χρήσης (ή μη έγκυρα) σημεία κωδικού Unicode ή χαρακτήρες γραμμής και πλαισίου. Ούτε μπορούν να ξεκινήσουν με έναν αριθμό, αν και οι αριθμοί μπορεί να περιλαμβάνονται αλλού στο όνομα.
Παράδειγμα:var 12 = "Apple.com" // δίνει ένα σφάλμα μεταγλώττισης: αναμενόμενο μοτίβο var @hello = "Γεια" // δίνει ένα σφάλμα μεταγλώττισης: αναμενόμενο μοτίβο