Skip to content

Instantly share code, notes, and snippets.

@movii
Last active August 29, 2015 14:06
Show Gist options
  • Save movii/1cdd6c00d962ea852107 to your computer and use it in GitHub Desktop.
Save movii/1cdd6c00d962ea852107 to your computer and use it in GitHub Desktop.
Swift 1. Core Syntax
// Defining Functions
// !!!
// in Swift, by default, an input parameter is constant, not variable
// for example
func paramsAreConstant( age: Int){
// age = age + 1 not allowed, because the age is constant
}
func paramsAreConstant_var(var age: Int){
// age = age + 1 now allowed, because the age is constant
}
func myFunction( name: String, age: Int){
println("This is a simple funciton.");
}
// to call
//myFunction()
// Functions that return value
func myFunciton_return() -> String{
return "hello";
}
// Using default parameter values
func myFunction_withDefaultValue(name : String = "John Doe"){
println("hello, \(name)");
}
// to call
myFunction_withDefaultValue() // Okay output "hello, John Doe"
// !!! myFunction_withDefaultValue() // ERROR - this will not work
myFunction_withDefaultValue(name : "Jane") // Okay output "hello, Jane"
// Using named parameters
func add(a:Int = 10, b: Int = 50){
println("The result is \(a+b)");
}
// add(99) ERROR, compiler error
add(a: 99);
add(b: 200);
add(a:99, b:200);
// Using for-in loops with Strings
var name = "Bob";
for eachChar in name{
println(eachChar);
}
//Creating for loops in Swift
//initializer ; condition; increment
for var i=0; i<100; i++ {
//
}
// Creating for-in loops in Swift
/*
for each-item in some-collection {
use each-item
}
*/
var total = 0
for index in 1...100{ //1...100 closed range operator
total = total + index
}
println("The total is \(total)")
// THE if STATEMENT
var myVar_ifStatement = 1;
if myVar_ifStatement > 500 {
//do somethings
}else if myVar_ifStatement < 500{
// do somethings
}
/*
1. ... closed range operator
0...100
36...99
0...someVariable
2. ..< half-open range operator
0..<100
36..<99
0..<someArray.count
the only difference is that the half-range operator does not include the number at the right side
*/
var str = "Hello, playground";
var highScore:Int = 1000;
highScore = highScore + 100;
for i in 0..<100 {
highScore = highScore + i;
}
var firstName:String = "Chip";
var isActive:Bool = true;
var myVariable: Int;
let daysInWeek = 7;
println("this is a print statement!");
//String Interpolation in Swift
let city = "Shanghai";
var day = "Saturday";
var temp = 75;
println("The hign for \(city) on \(day) is \(temp) degrees!");
// String Interpolation with Expression
var quantity = 17;
var unitPrice = 349;
println("The amount is \(quantity * unitPrice)");
//Expression using different TYPES
//Swift doesn't implicitely convert VALUES
var quantity_int: Int = 17; //Int
var unitPrice_double: Double = 349.54; // Double
//to convert, use Double(), Int(), String() etc
println("The amount is \(Double(quantity_int) * unitPrice_double)");
// Switch STATEMENT
let windSpeed = 5;
switch windSpeed {
case 0:
println("It couldn't be ");
// in the case it's 0, do this
case 1:
println("There's harldy a breeze.");
// in the case it's 1, do this
// (etc)
case 12 :
println("Hurricane! Batten down the hatches!")
// in the case it's 12, do this
// Swift - using ranges
case 0...3:
println("it's vary calm")
case 4...6:
println("A little windy");
default:
//all other value
break // to explicitly end this otherwise empty case
}
// Creating while loops in Swift
/*
while condition {
// do something
}
do{
// do something
} while condition
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment