codes

fukurou

the supreme coder
ADMIN
Code:
*variables*
var age = 0 // implicit var declaration
var isCompleted:Bool = false // explicit var declaration
let num:Int = 2 // const takes less space than var

*strings*
var str1:String = "1+4 = \(1+4)" // "1+4 = 5"

*classes*
class MyClass:SomeSuperClass{
 var myProperty:Int
 override init(){
  myProperty = 12
 }
 // methods
}

*methods*
func myMethod() -> Bool{
 return true
}
func methodWithParams(a:Int,b:Int){
 print(a + b)
}

*varargs*
func sum1(_ numbers: Int...) -> Int {
    var total = 0
    for number in numbers {
        total += number
    }
    return total
}
var test:Int = sum1(1,2,3)
print(test)

*arraylist*
var dClasses: Array<DiSkillV2> = [DiSkillV2]()

*array*
var elements: [T] = []
var numbers : [Int] = [2, 4, 6, 8]
var numbers = [2, 4, 6, 8]
var value = [Int]()
var languages = ["Swift", "Java", "C++"]

// access element at index 0
print(languages[0])   // Swift

*dictionary*
var dic1:[String:Int] = [:]
dic1["one"]=1;dic1["two"] = 2
for (str,num) in dic1{
    print("\(str)\(num)")
}

*if*
if true {print("true")}else{print("false")}
condition ? expression1 : expression2 // return exp1 if true

*loops*
for _ in 0...2 {print("hi")} // indexless
for i in 0...4 {print(i)}
for i in 0..<a.count {}
for item in sentences{}
while(Bool){}
repeat{ }
while(Bool)

*switch*
let nt = 1
switch (nt)  {
case 1:
    print("case 1")
case 2:
    print("case 2")
default:
    print("default")
}

*randomizer*
Int.random(in: 0...5) // includes 0 and 5
Int.random(in: 0..<5) // includes 0
array1.randomElement()! // returns a random element from the array

*N-dimensional arrays*
var cinemas = [[[Int]]]()
var cinemas = Array(repeating: Array(repeating: Array(repeating: 0, count: 5), count: 5), count: 3)
cinemaRooms[1][2][3] = 1 // the second-floor cinema, the third row, the fourth seat
 
Last edited:
Top