: Playground-noun:a place where people can play
Import UIKit
var str = "Hello, playground"
Let individualscores = [103, A., ]
var teamscore = 0;
For score in individualscores {
If the condition in the judge does not add ()
if score > {
teamscore + = 3;
} Else{
teamscore + = 1;
}
}
Print(teamscore);
//? Optional value can be nil
You can use if and let together to handle the case of missing values. These values can be represented by optional values. An optional value is either a specific value or nil to indicate that the value is missing. It is optional to add a question mark after the type to mark the value of the variable.
var optionalstring: String? = "Hello";
Print(optionalstring = = nil)
var optionalname:String? = nil;
var greeting = "Hello";
If let name = optionalname {
Greeting = "Hello, \(name)";
} Else if(optionalname = = Nil) {
Greeting = "Bye"
}
//?? Default Value
if the optional value of the variable is nil, the condition is judged to be false and the code in the curly braces is skipped. If it is not nil, the value is assigned to the constant of the let face so that the value can be used in the code block.
another way to process an optional value is by using the ?? operator to provide a default value. If the optional values are missing , you can replace them with the default values .
Let nickname: String? = nil;
Let fullName: String = "John Appleseed";
Let informalgreetin = "Hi \(nickname ?? fullName) ";
Switch supports any type of data and various comparison operations- - not just integers, but test equality.
Let vegetable = "red pepper";
Switch vegetable {
case "celery":
Print("Add some raisins and make ants on a log." );
case "cucumber","watercress":
Print("That would make a good tea sandwich." );
Case let x where x.hassuffix("Pepper"):
Print("is it a spicy \(x)");
default:
Print("Everyting tastes good in soup");
}
Let interestingnumbers = [
"Prime": [2, 3, 5, 7, one, +],
"Fibonacci": [1, 1, 2, 3, 5, 8],
"Square": [1, 4, 9, +, ],
];
var largest = 0;
var largestkind: String? = nil;
You can use for-in to traverse A dictionary, which requires two variables to represent each key-value pair. A dictionary is an unordered collection , so their keys and values end in any order of iteration.
for (kind, numbers) in interestingnumbers {
For number in numbers{
if number > largest {
largest = number;
largestkind = kind;
}
}
}
var n = 2;
While n < {
n = n*2;
}
Print(n)
var m = 2;
Repeat{
m = m*2;
}while m <
Print(m)
var firstforloop = 0
//.. < does not contain upper bound ... include upper bounds
can be used in loops : < to represent a range , or to use a traditional notation , which is equivalent :
For i in 0... 4 {
Firstforloop + = i;
}
Print(firstforloop)
var secondforloop = 0;
For var i = 0; i < 4; ++i {
Secondforloop + = i;
}
Print(secondforloop);
Swift 2.x learning Note (b)