Skip to content

Instantly share code, notes, and snippets.

@aimore
Last active June 21, 2016 10:48
Show Gist options
  • Save aimore/d3d856f8196e188a30deb5e01212c351 to your computer and use it in GitHub Desktop.
Save aimore/d3d856f8196e188a30deb5e01212c351 to your computer and use it in GitHub Desktop.
Swift basics ( 1 swift meetup)
//: Swift meetup 15:03.1026
import Cocoa
/*var int: Int=3
print(int)
var letter: [String] = ["a", "b", "c", "d", "e"]
print(letter)
letter.count
print("the var letter has \(letter.count) items")
letter.append("f")
var firstItem = letter[0]
letter[0] = "a"
var someInts = [Int]()
print("someInts is of type [Int] with \(someInts.count)")
someInts.append(3)
print("\(someInts.count) element, number: \(someInts)")
let c: Character = "a"
let s: String = "apples"
// ***** EMOJIS *****
/*for i in 0x1F601...0x1F64F {
var c = String(UnicodeScalar(i))
print(c)
}*/
/* NOTE: These ranges are still just a subset of all the emoji characters;
// they seem to be all over the place...
let emojiRanges = [
0x1F601...0x1F64F,
0x2702...0x27B0,
0x1F680...0x1F6C0,
0x1F170...0x1F251
]
for range in emojiRanges {
for i in range {
var c = String(UnicodeScalar(i))
print(c)
}
}
*/
// Emojis as variables
var 🙂 = 1
//number format
let avgTemp = 66.844322156
print(NSString(format:"%.2f", avgTemp))
//easy equal statement
let s1 = "string"
let s2 = "string"
let areEqual = s1==s2
// easy not equal statement
let areNotEqual = s1 != s2
//*** ARRAYS ***
// let(for constant you cannot modify it after declared) or var(as variable you can modify)
// let/var Arrayname = [put your elements into ""]
let shoppingListconstant = ["Queijo", "Merenda", "Chocolate", "Carne"]
//Arrays - Order of elements
// It started always the first element as "0"
let firstitem = shoppingListconstant [0]
// Declaring array as variable
var shoppingList = ["Schinken", "Brot", "Milch", "Banana"]
//Changing array values
shoppingList[1] = "Pizza"
// Now the second element (counting from 0) is now "Pizza" instead "Brot"
shoppingList
let questions: [String] = ["From what is cognac made?", "What is 7+7?", "What is the capital of Vermont?"]
let answers: [String] = ["Grapes", "14", "Montpelier"]
var currentQuestionIndex: Int = 0
*/
*/
var hasPet: Bool
var arrayOfInts: [Int]
var dictionaryOfCapitalsByCountry: [String:String]
var winningLoterynumbers: Set<Int>
//Arrays of Integers
let countingUp: [Int] = [1, 2, 3, 4, 5]
var listOfNumbers = [1, 2, 3, 1, 2, 10, 100]
let firstNumber = countingUp[0]
let secondNumber = listOfNumbers[1]
// Initializers
let emptyString = String()
let emptyArrayOfInts = [Int]()
let emptySetOfFloats = Set<Float>()
// Default
let defaultBool = Bool()
let meaningOfLife = String(listOfNumbers)
//Porperties
//count, description, end and start index
var supermarketList: [String] = ["Bread", "Milk", "Eggs"]
supermarketList.description
supermarketList.count
supermarketList.endIndex
supermarketList.startIndex
//check if the string is empty
let EmptyString = ""
EmptyString.isEmpty
//Instance Methods
var CountingUp = ["one", "two", "three", "four", "five"]
let secondElement = [1]
CountingUp.count
CountingUp.append("six")
//Optionals
var anOptionalFloat: Float?
var anOptionalArrayOfStrings: [String]?
var anOptionalArrayOfOptionalString: [String?]?
// Working with Float
var readingOne: Float?
var readingTwo: Float?
var readingThree: Float?
readingOne = 9.8
readingTwo = 9.2
readingThree = 9.7
if let r1 = readingOne,
r2 = readingTwo,
r3 = readingThree {
let avgReading = (r1 + r2 + r3) / 3
} else {
let errorString = "Instrument reported a reading that was nil"
}
//subscripting dictionaries
let nameByparkingSpace = [13:"Alice", 25:"Paulo"]
if let space13Assignee = nameByparkingSpace[13]{
print("Key 13 is assigned in the dictionary")
}
//Loops and String Interpolation
for i in 0 ..< countingUp.count {
let string = countingUp [1]
}
//: Playground - noun: a place where people can play
import UIKit
/*
// Challenge 1
var int = 123 * 456
//Types Constants (let) and Variables (var)
//constant (let)
let aimore = "meu nome"
//int (integers)
let number: Int = 10
//variables (var)
//double (decimal numbers)
var decimal: Double = 2.6
//bool (boolean is a value that can be true, or false)
var booleano: Bool = true
//string (letters or words)
var string: String = "this is a string"
//Challenge 2
var favoriteGame = "Fifa Ultimate Team 16"
favoriteGame = "FIFA"
//Challenge 3
let favNumber: Int = 7
// Comparison operators
let aimoreCoolness = 10
var lisaCoolness = 9
var robertCoolness = 1
var petraCoolness = 11
// < Less than
aimoreCoolness < lisaCoolness
// > Greater than
lisaCoolness >= 8
// == equal to
aimoreCoolness == (robertCoolness + lisaCoolness)
// & and
aimoreCoolness > robertCoolness && aimoreCoolness == (robertCoolness + lisaCoolness)
// || or
aimoreCoolness < lisaCoolness || robertCoolness < lisaCoolness
//Challenge 4
aimoreCoolness < petraCoolness
petraCoolness >= 12
petraCoolness == 11
aimoreCoolness > robertCoolness && petraCoolness == (lisaCoolness + robertCoolness)
aimoreCoolness < lisaCoolness || petraCoolness > lisaCoolness
// If and Else Statements
if (aimoreCoolness > petraCoolness) {
petraCoolness == petraCoolness - 1
}
else if (aimoreCoolness <= petraCoolness) {
petraCoolness == petraCoolness - 1
}
else {
petraCoolness == petraCoolness + 1
}
//Simple functions
//print()
print("Hello World")
print(aimore)
print(2+2)
print("Aimoré coolness is \(aimoreCoolness)")
//Challenge 5
var student1 = "Doquinha"
var student2 = "Coxinha"
var notaDoquinha = 107
var notaCoxinha = 107
if (notaCoxinha > notaDoquinha) {
print(student2 + " tem a maior nota")
}
else if (notaDoquinha > notaCoxinha) {
print(student2 + " tem a menor nota")
}
else {
print(student2 + " e " + student1 + " tem notas iguais")
}
// While Loops
var secondsLeft = 5
while (secondsLeft > 0) {
print(secondsLeft)
secondsLeft -= 1
}
print("Blast off!")
// Challenge 6
var donutsLeft = 8
while (donutsLeft > 0) {
print("You have \(donutsLeft) donuuuutis")
donutsLeft -= 2
}
print("You have no donuts braa!")
// example loop
var donutsleft = 6
while(donutsleft > 0) {
print("You have \(donutsleft) donuts left")
donutsleft = donutsleft - 1
print("You eat one donut")
}
print("You are all out of donuts :(")
// break statement
var cokesLeft = 7
var fantasLeft = 4
while(cokesLeft > 0) {
print("You have \(cokesLeft) Cokes left.")
cokesLeft = cokesLeft - 1
if(cokesLeft <= fantasLeft) {
break
}
}
print("This is how 'break' works.")
//Challenge 7
var goofingOff = 10
var bossStopsBy = 0
while (goofingOff > 0) {
print("you have \(goofingOff) minutes left")
goofingOff -= 1
bossStopsBy += 1
if (goofingOff == bossStopsBy){
break
}
}
print("The boss is comming!")
//example break loop
var goofOffTime = 10
var bossComing = 0
while(goofOffTime > 0) {
print("Goofing off!")
goofOffTime = goofOffTime - 1
bossComing = bossComing + 1
if(goofOffTime == bossComing) {
break
}
}
print("Done goofing off.")
*/
// Continue Statement
var numbers = 0
while(numbers <= 10) {
if(numbers == 9) {
numbers = numbers + 1
continue
}
print(numbers)
numbers = numbers + 1
}
// chalenge 8
var numeros = 1
while(numeros <= 11) {
if(numeros == 10) {
numeros = numeros + 1
continue
}
print(numeros)
numeros = numeros + 1
}
// example
print("List of World Wars:")
var ww = 1
while(ww <= 5) {
if(ww == 3 || ww == 4) {
ww = ww + 1
continue
}
print("World War \(ww)")
ww = ww + 1
}
print("That's the beauty of World War V, Lois. It's so intense it skips over the other two. - Peter Griffin")
// Optionals (nil)
var optionalNumber: Int? = 5
optionalNumber = nil
//chalenge 9
if let number = optionalNumber {
print("It's a number")
} else {
print("It's not a number") }
//Conversion Between Data Types
var languagesLearned: String = "Three"
var languagesLearnedNum: Int? = Int(languagesLearned)
//example
if let num = languagesLearnedNum {
print("it's a number")
} else {
print("It's not a number")
}
//Challenge 10
var myFirstVariable: String = "10"
var mySecondVariable = 12
var variableNum: Int? = Int(myFirstVariable)
if let numVariable = variableNum {
if variableNum > mySecondVariable {
print("\(variableNum) is greater than \(mySecondVariable)")
} else if (numVariable < mySecondVariable) {
print("\(numVariable) is less than \(mySecondVariable)")
} else {
print("\(numVariable) is equal to \(mySecondVariable)")
} }
else {
print("Invalidy entry")
}
// method that gives you a random number between a high and low number
func randomInBetween(low:Int, high:Int) -> Int {
let range = high - (low - 1)
return (Int(arc4random()) % range) + (low - 1)
}
// Time to Make a Game!
// set the answer random between 1 adn 100
let answer = randomInBetween(1, high:100)
// Tell the user what to do
print("Enter a number between 1 and 100")
//user's guess (variable)
var guess = 7
if(answer < guess) {
print("\(answer) is less than \(guess)")
}else if(answer > guess) {
print("\(answer) is greater than \(guess)")
}else {
print("acertou mizeravel")}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment