Answer:
let:
We use let keyword to define a constant.It can not be changed afterwards.
Example:
let Cvalue = 42
If you change a constant ,it will give an error as you cannot change the value of a constant.
Cvalue=58 (It will not compile).
var:
We use var keyword to define a variable whose value we can easily change.
Example:
var number1=45
number1=100;
Now, number1 value is changed to 100.
2.Why we use Type Annotations in Swift ?
Answer:
We provide a type annotation when we declare a constant or variable to identify the kind of values the constant or variable can store.
How to use ?
We write a type annotation by placing a colon after the constant or variable name followed by a space and followed by the type to use
Example:
var Message: String
The Message variable can now be set to any string value without error:
welcomeMessage = "Hello world"
Example of Character Annotation:
let exclamationMark: Character = "!"
3.How to Print Constants and Variables in Swift ?
Answer:
var Stringvalue="india!"
print(Stringvalue)
Prints "india!"
Swift uses string interpolation to include the name of a constant or variable as a placeholder in a longer string.
print("The name of my country is \(Stringvalue)")
Prints "The name of my country is India!"
4.How to use Comments in Swift ?
Answer:
Swift compiler ignores comments while compiling your code.
Comments in Swift are very similar to comments in C.
Single Line Comment:
// example of Swift's single line comment
Multiline comments ( It starts with (/*) and ends with (*/))
/* this is a multiline comment,
and it's used for multiline texts or codes */
Nested Multiline Comments:
/* starting a first multiline comment
/* Starting a second nested multiline comment */
Ending the first multiline comment */
5.Can you use Semicolon in Swift ?
Answer:
Yes, you can use semicolon in Swift.You don't need to write a semicolon (;) after the end of each statement in Swift.However,it's required if you write multiple separate statements on a single line.
Example:
let myName= "Morgan"; print(myName)
6.How to initialise an empty string in Swift ?
Answer:
var myEmptyString = ""
How to initialise an empty string using Initialisers?
var MyNewEmptyString = String()
How to find out whether the string is empty ?
if myEmptyString.isEmpty {
print("Yes ,String is empty")
}
prints "Yes ,String is empty"
7.How to use Mutable String in Swift ?
Answer:
String can be modified (or mutated) by assigning it to a variable.
var myString = "He is a"
newString += " good developer"
newString is now "He is a good developer"'
8.How to get individual characters from a string in Swift ?
Answer:
for char in "iPhone".characters {
print(char)
}
prints
// i
// P
// h
// o
// n
//e
9.What are the value of count1 and count 2 ?
var iOSArray1 = ["iOS","Xcode","Objective C", "Swift","Cocoa touch"]
var iOSArray2 = iOSArray1
iOSArray2.append("Storyboard")
var count1 = iOSArray1.count
var count2 = iOSArray2.count
Answer: 5,6
Read:
Top iOS Interview Questions And Answers for Beginners
Top 5 Simple iOS Interview Questions And Answers
Most Important iPhone interview Questions and Answers
Top iOS interview Questions And Answers Collection