19 June, 2014

Swift features & data types

Differences between Swift and established languages

  • no main function
  • no need to import libraries for common functions such as working with strings.
  • no need to put a semi-colon to mark the end of a statement.
  • enums can contain properties/methods
other notable features:
  • data types can be inferred

  • constructor: init() { }

  • answers.insert("You cannot do this alone", atIndex: 3) answers.removeAtIndex(4)

  • property: The simplest way to think of a property is that it is a class variable that has a getter and/or setter, but the accessor methods are provided by the system and there is no need to write these methods unless you wish to customise them.

  • Example: * A Label so that the Controller can update this Label with some data from the Model after a user clicks on the button.

Optional: is a special data type in Swift that allows the variable to have a 'nil' value. It can be declared either through the use of a '?' or and '!'. If declared with a '?' then if we want to access a value or property we have to unwrap the type using the '!'.

Example of not having to use an optional: If you declare a property that references the Model as 'Optional' because we use the 'initializer' to instantiate the model and therefore it is not at risk of being 'nil'.

  • Both Arrays and Dictionaries are type-safe, meaning that we cannot mix data types. (though, you can with tuples)

Which data structure should I use?

array - unordered list

Dictionary - key value pair. let numbers = ["one":"uno","two":"two"] problem: Can only return 1 value form a function..so how will we return both values?

Set properties on the Model. Good option if you want to maintain this data as part of the state of the Model so you can continue to access these values later in the lifecycle of the program.

Pass parameters by reference. If you don't want to maintain the data required as part of the state of the Model, but are more interested in local scope within a particular method. Issue is, we need to pass the same number of parameters as we need returned. So that might lead to passing in empty parameters. This approach is more appropriate for local scope when you have variables which you wish to pass t the Model and have the variables changed by the Model.

Return a data structure. If you do not wish to maintain the data required as part of the state of the# Model, but are more interested in local scope within a particular method.

and some more data structures...

data structures

The best approach is to start with a Enum, then if you find this is not suitable upgrade to a Struct, and finally upgrade to Class where necessary.

Guidelines for when to choose enum to hold your data

  1. If you wish to model an object that has a finite number of representations. eg. only 52 cards
  2. If a particular representation will determine the state of the object. eg. 'fools card' always represented by the number 1
  3. If all the methods in the object are focused only on the state of the object. (I don't want my card object for example too refer to other objects in my application or to try modify other objects in the system. )
  4. If you do not need to have a stored property.
  5. If you do not want to pass the object by reference (by default)

Note: enums only have calculated properties ( no stored properties). Values must always be retrieved by a getter.

** Enum vs Struct ** They both can contain Properties, Initializers, and Methods. They're also both pass by value..so whats the difference?

A 'Struct' in Swift differs from an 'Enum' in that it can have stored properties as well as calculated properties. A 'Struct' is more suited to modelling an object that does not have a finite number of representations and can have those properties modified independently of from one another. A 'Struct' though is still pass by value, so is not suited to objects that you wish to hold references to other objects or you wish to pass by reference.

When to use a Struct

The structure’s primary purpose is to encapsulate a few relatively simple data values. Any properties stored by the structure are themselves value types, which would also be expected to be copied rather than referenced. You do not need to use inheritance to create a class hierarchy. You do not need the data structure to be passed by reference if you need to modify the properties of your structure within a particular method, you can opt in to mutating behavior for that method.

loops

The for-in loop performs a set of statements for each item in a range, sequence, collection, or progression. The for loop performs a set of statements until a specific condition is met, typically by incrementing a counter each time the loop ends.

Singleton pattern

The singleton pattern basically ensures that your application only ever instantiates one model throughout the lifetime of the application. All parts of your program share this model. If you make a change in your model in one part of your application, when you visit another part, it too will know about this change because everyone is sharing the same copy of the model.

The essence of the pattern is that you have a private constructor so it cannot be instantiated directly. You then have a private instance variable that holds itself. Finally you provide a static method that allows a caller to ask for the model. The method simply checks if the private instance variable has been initialised. If it has, it returns the model. If it hasn't it first initialises the model and then returns it to the caller. In other words the model is only ever instantiated once.