Swift – Enumerations and Structures ( Part 5 )

enum for enumerations

In Swift, enumerations can have methods like all other name types. In enumerations, the list have raw values and All you have to do is defining first raw value. The rest of the raw values will be assigned in order. You can reach this raw value by calling rawValue property.

Defining your enumeration

enum BeatlesCollection : Int {
    case AHardDaysNight = 1964
    case Help, YesterdayAndToday, MagicalMysteryTour, TheBeatles, YellowSubmarine
    case LetItBe
    func yearDescription() -> String{
        switch self{
        default :
            return String(self.rawValue)
    // You can add additional functions here.

Creating variable from an enumeration value

let anAlbumOfBeatle = BeatlesCollection.LetItBe

Screen Shot 2016-02-06 at 20.17.45

struct for structures

Structures are like classes, they can have methods, initializers and properties. But, when you pass a structure to a method, it behaves differently, structures are passed by value, classes are passed by reference.

struct BeatlesAlbum {
    var yearOfAlbum: BeatlesCollection
    var salesRecord : Int
    var albumName : String
    init() {
        self.yearOfAlbum = BeatlesCollection.AHardDaysNight
        self.salesRecord = 0
        self.albumName = ""
    init(albumName : String, salesRecord : Int, albumYear : BeatlesCollection) {
        self.yearOfAlbum = albumYear
        self.salesRecord = salesRecord
        self.albumName = albumName
    func simpleYearAndSalesRecordDescription()->String{
        return albumName + " was released in " + self.yearOfAlbum.yearDescription() + " and It reached \(salesRecord) number of sales record."

let help = BeatlesAlbum(albumName: "Help", salesRecord: 2303500000 , albumYear: BeatlesCollection.Help)

Screen Shot 2016-02-06 at 21.07.17

See you in another chapter!
You can reach the full tutorial here!


One thought on “Swift – Enumerations and Structures ( Part 5 )

  1. […] Part 1 – Define variables, constants and simple object types – Swift Hello World! Part 2 – Use if, switch, for, for-in, while, repeat-while statements – Control Flows Part 3 – Functions & Closures – How to call and define them Part 4 – Objects & Classes – get, set, willSet and didSet methods of Properties Part 5 – Enumerations & Structures – enum & struct in Swift […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s