Skip to content

Instantly share code, notes, and snippets.

@0xjorgev
Last active March 30, 2017 04:20
Show Gist options
  • Save 0xjorgev/3703c19a59e65f3db66b to your computer and use it in GitHub Desktop.
Save 0xjorgev/3703c19a59e65f3db66b to your computer and use it in GitHub Desktop.
This is a Quick Swift Reference Guide for TheSwift.ninja
// Playground - noun: a place where people can play
import Foundation
import UIKit
//Implicit inmutable variable
let dojoNinjas = 1
//Implicit mutable variable
var ninjaApprentices = 1500
//Explicit inmutable variable
let masterNinjas:Int = 0
//Explicit mutable variable
var ninjaStars:Int = 500
//Implicit String variable
var secrettNinjaMessage = "Do or do not, there is no try!"
//Explicit String variable
var faketNinjaMessage:String = "Fear is the path into the Darkside"
//Arrays
//Constant Implicit Array of Strings
let ninjaWeapons = ["knife", "Swords", "Say", "Bo", "daggers", "stars", "Bow and Arrow"]
//Explicit Array of Strings
var ninjaMoves:[String] = ["Dodge", "Hand Attack", "Kick", "Hide", "Jump", "Fatality"]
//Array Cont
let weaponsInventory:Int = ninjaWeapons.count
//Array Object at index 2, position 3 (index starts with 0)
var favoriteWeapon:String = ninjaWeapons[2]
//Add new element to the array
ninjaMoves.append("Smoked Ilutions")
//Object at index 2, position 3
ninjaMoves[2]
//CONTROL FLOW
//Compare between Integer values
if weaponsInventory > 0 {
println("Ready to Attack")
} else {
println("Hide instead")
}
var enemiesHitedWithArrows:Int = 0
//Flow control in For Loops
for arrow in 0 ... 99 {
let posibility:Float = Float(arc4random_uniform(2))
//Posibles values 0 and 1
if posibility == 1 {
enemiesHitedWithArrows++
}
println("You've thrown \(arrow) arrows and hitted \(enemiesHitedWithArrows) ninja enemies ")
}
1 == 1 // true, because 1 is equal to 1
2 != 1 // true, because 2 is not equal to 1
2 > 1 // true, because 2 is greater than 1
1 < 2 // true, because 1 is less than 2
1 >= 1 // true, because 1 is greater than or equal to 1
2 <= 1 // false, because 2 is not less than or equal to 1
let hitCounter = 35
switch(hitCounter){
case 0 ... 5 :
println("Time to strat attacking!")
case 6 ... 10 :
println("Time to Jump!")
case 10 ... 35 :
println("Fatality attack time")
default:
println("Calm your mind and meditate your position")
}
//Enumeration
enum attack {
case fast
case silence
case deadly
}
let myNinjaAttack:attack = .fast
//Switch and Enums
//This is an untyped enum
switch (myNinjaAttack){
case .fast:
println("Fast as an eagle Attack!")
case .silence:
println("Silent as a snake attack!")
case .deadly:
println("The Black Mamba attack!")
}
//Simple Void Function
func simpleNinjaFunction(){
println("This is prety much it ... 👍")
}
//Void Function with Parameters
func parameterFunction(att:attack){
println("The attack description is \(att)")
}
//Type String Function with Parameters
func ninjaAttackDescription(att:attack) -> String {
switch (myNinjaAttack){
case .fast:
return "Fast as an eagle Attack!"
case .silence:
return "Silent as a snake attack!"
case .deadly:
return "The Black Mamba attack!"
}
}
//Type Int Function with Parameters
func numberOfWeapons(weapons:[String]) -> Int {
return weapons.count
}
//Classes
class Ninja:NSObject {
let secrettMessage:String
let weapons:[String]
let moves:[String]
var favoriteWeapon:String
var color:UIColor
var attackStyle:attack
var arrowInventory:Int
init(message:String, weapons:[String], moves:[String],favWeapon:String, color:UIColor, style:attack, arrows:Int) {
self.secrettMessage = message
self.weapons = weapons
self.moves = moves
self.favoriteWeapon = favWeapon
self.color = color
self.attackStyle = style
self.arrowInventory = arrows
}
func setFavWeapon(index:Int){
self.favoriteWeapon = weapons[index]
}
func setColor(color:UIColor){
self.color = color
}
func performRandomMove() ->String {
let index:Int = Int(arc4random_uniform(UInt32(self.moves.count)))
return self.moves[index]
}
func throwArrow() -> Bool {
let posibility:Float = Float(arc4random_uniform(2))
//Posibles values 0 and 1
self.arrowInventory--
if posibility == 1 {
return true
} else {
return false
}
}
func throwArrowAttack() ->Self {
self.arrowInventory--
return self
}
func printArrowsLeft(){
println("Arrows lefts: \(self.arrowInventory)")
}
}
//Class initialization
var eliteNinja:Ninja = Ninja(message: secrettNinjaMessage, weapons: ninjaWeapons, moves: ninjaMoves, favWeapon: ninjaWeapons[2], color:UIColor.clearColor(), style: attack.silence, arrows:50)
eliteNinja.setColor(UIColor.blueColor())
eliteNinja .throwArrow()
//Print the amount of remaining amount of arrows
eliteNinja.printArrowsLeft()
//Continuos attacks!
eliteNinja.throwArrowAttack().throwArrowAttack().throwArrowAttack().throwArrowAttack()
eliteNinja.printArrowsLeft()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment