Last active
February 9, 2021 10:47
-
-
Save retrokid/931134fdef0d825810d1145d2fd6df2e to your computer and use it in GitHub Desktop.
Swift var-let-array-dict
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Created by efe ertugrul on 18/08/16. | |
import Cocoa | |
// VAR - LET - ARRAY - DICTIONARY | |
// constants | |
let anInt : Int = 1 | |
let aDouble : Double = 1.0 | |
let aFloat : Float = 1.0 | |
let aString : String = "bir" | |
// optional variables can be 'nil' | |
var anOptionalInt : Int? = 1 | |
var anOptionalDouble : Double? = 1.0 | |
var anOptionalFloat : Float? = 1.0 | |
var anOptionalString : String? = "bir" | |
// empty array and dictionary | |
var anArray : [Int] = [] | |
var aDictionary : [String:String] = [:] | |
// optional empty array and dictionary | |
var anOptionalArray : [Int?] = [] | |
var anOptionalDictionary : [String:String?] = [:] | |
// add item to array | |
anArray.append(0) | |
anArray.append(1) | |
anArray.append(2) | |
// add item to optional array | |
anOptionalArray.append(0) | |
anOptionalArray.append(nil) | |
anOptionalArray.append(2) | |
// add item to dictionary | |
aDictionary["0"] = "sıfır" | |
aDictionary["1"] = "bir" | |
aDictionary["2"] = "iki" | |
// add item to optional dictionary | |
anOptionalDictionary["0"] = "sıfır" | |
anOptionalDictionary["1"] = nil | |
anOptionalDictionary["3"] = "üç" | |
// mixed array and dictionary | |
var mixedArray = [Any]() | |
var mixedDictionary = [String: Any]() | |
// add item to mixed array | |
mixedArray.append(0) | |
mixedArray.append("bir") | |
mixedArray.append(1.25) | |
mixedArray.append([334,55,33,22]) | |
mixedArray.append(["ssd":[33,"ddds",445]]) | |
// add item to mixed dictionary | |
mixedDictionary["0"] = 15 | |
mixedDictionary["1"] = "bir" | |
mixedDictionary["2"] = [33:45] | |
mixedDictionary["3"] = [22,34,33,"ksks"] | |
mixedDictionary["4"] = ["current":[23,55,3,44,2]] | |
// optional mixed array and dictionary | |
var optionalMixedArray = [Any?]() | |
var optionalMixedDictionary = [String:Any?]() | |
// add item to optional mixed array | |
optionalMixedArray.append(0) | |
optionalMixedArray.append("bir") | |
optionalMixedArray.append(nil) | |
optionalMixedArray.append([33,44,33,22]) | |
optionalMixedArray.append(["ssd":[33,"ddds",445]]) | |
// add item to optional mixed dictionary | |
optionalMixedDictionary["0"] = 15 | |
optionalMixedDictionary["1"] = nil | |
optionalMixedDictionary["2"] = [33:33] | |
optionalMixedDictionary["3"] = [22,34,33] | |
optionalMixedDictionary["4"] = ["current":[23,55,3,44,2]] | |
// printing | |
print("anArray : \(anArray)") | |
print("aDictionary : \(aDictionary)") | |
print("anOptionalArray : \(anOptionalArray)") | |
print("anOptionalDictionary : \(anOptionalDictionary)") | |
print("mixedArray : \(mixedArray)") | |
print("mixedDictionary : \(mixedDictionary)") | |
print("optionalMixedArray : \(optionalMixedArray)") | |
print("optionalMixedDictionary : \(optionalMixedDictionary)") // doesn't print 'nil's !! | |
print("---------") | |
print(anArray) | |
print(aDictionary) | |
// delete an item from array and dictionary | |
anArray.remove(at: 0) | |
aDictionary.removeValue(forKey: "0") | |
print("---------") | |
print(anArray) | |
print(aDictionary) | |
// change an item in array or dictionary | |
anArray[0] = 33 | |
aDictionary["0"] = "dkdkdkdk" | |
print("---------") | |
print(anArray) | |
print(aDictionary) | |
// sort array items | |
anArray.sort() // incremental | |
anArray.sort( isOrderedBefore: { $0 > $1 } ) // decremental | |
// check if an element exists | |
anArray.contains(33) | |
print("---------") | |
print(anArray) | |
print(aDictionary) | |
// delete all items in array and dictionary | |
anArray.removeAll() | |
aDictionary.removeAll() | |
print("---------") | |
print(anArray) | |
print(aDictionary) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment