Viewing entries in
iOS development

Swift Collections

Comment

Swift Collections

There are three main collection types in Swift:

  • Arrays
  • Sets
  • Dictionaries

All container types support the following:

  • count
  • isEmpty
  • append(item)

Arrays

Example array usage:

brush: swift
var array1 : Array<Int> = Array<Int>() // Full type name
var array2 : [Int] = [Int]()           // Shorthand form
var array3 : Array<Int> = []
var array4 = Array<Int>()
var array5 = [Int]()                   // Empty constructor
var array6 = [0, 1, 3, 7]              // Type inference: Array<Int>

var myArray Array<Int> = [Int]() // Empty array
var zeroes = [Int](count: 100, repeatedValue: 0)

myArray.append(5)

// I'm hungry. Let's make a PBJ
let pbj = ["peanut butter", "jelly", "bread"]

// Oops! Need to buy stuff
var shoppingList : [String]
shoppingList = shoppingList + pbj

shoppingList[0..2] = ["potatoes", "chips", "more potatoes"]

if !shoppingList.isEmpty {
  // Go shopping
}

// Once in supermarket
var shoppingCart : Array<String>
for grocery in shoppingList {
  shoppingCart.append(grocery)
}
// Don't forget the most important thing!
shoppingCart.insert("", atIndex: 0)

var fancyArray: [FancyClass]

for (index, fancyItem) in fancyArray.enumerate() {
  print("\(index\): \(fancyItem.fancyName()\)")
}

for fancyItem in fancyArray {
  getRidOf(fancyItem)
}

Interesting member functions:

  • isEmpty
  • append(item)
  • insert(item, atIndex: n)
  • removeAtIndex(index)
  • removeLast(): rather than using removeAtIndex(a.count)

Sets

Example set usage:

brush: swift
var letters : Set<Character>() = ["a", "b", "c"]
var letters : Set = ["a", "b", "c"]

for letter in letters.sort() {
  print("\(letter\)")
}

Interesting functions:

  • isEmpty
  • insert(item)
  • remove(item)
  • contains(item)
  • sort()
  • intersect(set)
  • exclusiveOr(set)
  • union(set)
  • subtract(set)
  • isSubsetOf(set)
  • isSupersetOf(set)
  • isStrictSubsetOf(set)
  • isStrictSupersetOf(set)
  • isDisjointWidth(set)

Interesting properties:

  • count

Dictionaries

Example dictionary usage:

brush: swift
var emptyDictionary : [Int: String]()

var cityPopulation = {}
cityPopulation["Pompeya"] = nil // Remove element after eruption

var airports = ["YYZ": "Toronto Pearson", "DUB": "Dublin"] // [String: String]

Interesting functions:

  • keys
  • values

Comment

Anatomy of a Swift class

1 Comment

Anatomy of a Swift class

So, let's get started with Swift. A basic class looks likes this:

brush: swift
class PersonQueue : ParentClass, Protocol1, Prot2 {

  // Class constants
  // Not currently supported by Swift

  // Member variables
  var numberOfPersons = 3  // Initialized here
  var creatorName : String // To be initialiazed in init

  var enabled : Bool  = true {
     willSet (newVal) {
     }
     didSet {            
     }
  }

  // Computed properties
  var count : Int {
     set (newValue) { // newValue is default and can be omitted
        numberOfPersons = newValue
     }
     get {
        return numberOfPeople
     }
  }
  var overflow : Bool { // get is omitted
    return numberOfPeople > 5
  }

  lazy var background : UIImage = UIImage.fromFile()

  // Instance constants
  let other : Int = 0
  let id : Int  // Must be initialized in init

  // Initializer (constructor in C++)
  // Must assign all instance variables
  init(creator : String) {
    self.creatorName = creator
    self.id = 0
  }

  // A member function
  func getCreator() -> String {
    return self.creatorName
  }

  // A class function
  static nameOfClass() -> String {
    return "This class has no name"
  }
}

I hope I can remember all this syntax... I'm getting too old to keep all this in my brain.

1 Comment

Comment

UITextView font size not working

Be careful! You cannot set the font size of a UITextView from Interface Builder if "selectable" is not enabled. You will have to do this programmatically!

Found the answer to my problem in this Stack Overflow link.

Comment