Created
May 19, 2017 09:49
-
-
Save davidkneely/940c0ff3454c8d0ac6bd31d5eb003d31 to your computer and use it in GitHub Desktop.
Write up for Hawaii iOS Developer Meetup: Enums, Structs, and Classes
This file contains 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
//: Playground - noun: a place where people can play | |
import UIKit | |
var str = "Hello, playground" | |
/* | |
Notes form last week to review before tonight's session: | |
Research reduce to start next meetup with answer to Joanne's question. | |
Reduce: | |
Combine the elements of an array into a single value. | |
*/ | |
let moneyArray = [1,2,3,4,5] | |
var sum = 0 | |
// method that adds up numbers | |
for money in moneyArray { | |
sum = sum + money | |
} | |
sum | |
// method that multiplies numbers | |
var product = 1 | |
for money in moneyArray { | |
product = product * money | |
} | |
product | |
// NOTE: The only 2 differences between the 2 for loops is that one is computing the sum and one is computing the product and 2) the starting point we are using: 0 for sum and 1 for product. | |
// Reduce is a method that lets us quickly define this kind of operation by specifying an initial value and a method of combining elements. | |
// Here's the reduce function definition: | |
// reduce(initial: U, combine: (U,T) -> U) -> U | |
// U denotes the types that will be combined | |
// T denotes a single element of type U | |
// The whole array will be reduced to a type U | |
sum = moneyArray.reduce(0, {$0 + $1}) | |
sum | |
// and we can write it this way too | |
sum = moneyArray.reduce(0) {$0 + $1} | |
sum | |
// condense the closure even further | |
sum = moneyArray.reduce(0,+) | |
/* | |
classes and closures are reference types. | |
structs and enums are value types. | |
good to pass around, because you get a new copy of that | |
Class vs. Struct vs. Enum | |
// Enums | |
Initialized by one of a finite number of cases, and should always be valid instances of that case when instantiated. | |
// Structs | |
Should be used when there is not a finite number of valid instances (e.g. enum cases) and the struct also does not form a complete definition of an object, but rather an attribute or object. | |
// Classes | |
Completely defines a primary actor in your object model, both its attributes and its interactions. | |
source: swift-studies.com | |
Deep discussion: | |
Enums | |
Standalone: | |
Represent a finite number of "states of being" | |
e.g.: Return value as enum describes the exit state of the function. | |
Assembled: | |
Describes a sub-set of the state of an object. | |
Creating functions that do things in enums makes them more like a classes | |
GOTCHA: Make sure all functions and states can be derived by the state of each enum state. | |
REMEMBER: Enums are initialized by their case and should always be valid instances of the case when instantiated. | |
Structs | |
Think of it as a base type that cannot be extended. //TODO: research for how it applies to inheritance. Cannot have a subclass of a struct. | |
While functions can be performed on structs they should only provide additional information about that type. | |
Make sure it is type defined by the assembly of its attributes. | |
// can do mutations on a struct but any mutations are only applied to that struct. | |
Considerations: | |
1) All functions either return information about the instance (for example, the centre of a CGRect as a CGPoint) or provide convenience functionality for modifying the instance (e.g. scaling a rect) | |
2) Functions should not manipulate the state of other objects (classes or structs). | |
3) None of the attributes of the struct require explicit releasing (e.g. observers) // because they are value typed. | |
4) Inheritance is not required. | |
// When things are value type, there are no reference counts on it. | |
If extending structs be sure to conform to the 1 and 2 above // specifically using the extend keyword. | |
Structs should be used when there is not a finite number of valid instances that also does not form a complete definition of an object. | |
Classes | |
Classes collaborate as primary components of a network that forms the view of a model of your application (or aspect of your application). | |
Functions on classes could have behavior that manipulates the view or model of your application outside of the scope of the class and its attributes (i.e. Might manipulate other objects in the object model). | |
Try to minimize the number of classes if possible. | |
Encapsulate behaviors pertaining to their attributes inside structs, and descriptive attributes inside enums. | |
Classes completely define a primary actor in your object model, both its attributes and its interactions. | |
TAKEAWAYS: | |
Which one is best? What's the best way to decide? | |
Start with enum, then to struct, then to classes. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment