Swift create and initialize arrays


import UIKit

//to define an immutable array use let keyword
let array1 = [1,2,3]

//to define a mutable array use var keyword
var array2 = [4,5,6]

//define an empty mutable array of integers
var array3: [Int] = []

//create arrays of various data types
var array4: [String] = []
var array5: [Double] = []

struct MyObject {
    let name: String
    let age: Int
}
var array6: [MyObject] = []

//array of mixed objects
var array7: [Any] = [1,"Two", 2.0]

//multi dimensional array
var multiArray1 = [[1,2],[3,4],[5,6]]
var multiArray2 = [[1,"One"],[2,"Two"],[3,"Three"]]


No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.