Skip to content

Instantly share code, notes, and snippets.

View jgsamudio's full-sized avatar

Jonathan Samudio jgsamudio

View GitHub Profile
@jgsamudio
jgsamudio / Swift4.1EnumExample.swift
Created June 12, 2018 01:55
Swift 4.1 Enum Example
enum Fruits: String {
case apple
case banana
case strawberry
case kiwi
static var allCases: [Fruits] {
return [.apple, .banana, .strawberry, .kiwi]
}
@jgsamudio
jgsamudio / CaseIterableProtocol.swift
Created June 12, 2018 01:57
Case Iterable Protocol
public protocol CaseIterable {
/// A type that can represent a collection of all values of this type.
associatedtype AllCases : Collection where Self.AllCases.Element == Self
/// A collection of all values of this type.
public static var allCases: Self.AllCases { get }
}
@jgsamudio
jgsamudio / CaseIterableEnumExample.swift
Created June 12, 2018 01:58
Swift 4.2 Enum Example
enum Fruits: String, CaseIterable {
case apple
case banana
case strawberry
case kiwi
static func printFruits() {
for fruit in Fruits.allCases {
print(fruit)
}
@jgsamudio
jgsamudio / UserEquatableExample.swift
Last active June 12, 2018 02:08
User Equatable Example
struct User: Equatable {
let userId: String
let email: String
let lovesFruits: Bool
static func == (lhs: User, rhs: User) -> Bool {
return lhs.userId == rhs.userId &&
lhs.email == rhs.email &&
lhs.lovesFruits == rhs.lovesFruits
}
@jgsamudio
jgsamudio / UserEquatable4.2Example.swift
Created June 12, 2018 02:09
Swift 4.2 User Equatable Example
struct User: Equatable {
let userId: String
let email: String
let lovesFruits: Bool
}
@jgsamudio
jgsamudio / ArrayConditionalConformance.swift
Created June 12, 2018 02:11
Array Conditional Conformance
struct UserInfo {
let userArray1 = [User(userId: "1", email: "something@gmail.com", lovesFruits: true),
User(userId: "2", email: "something@gmail.com", lovesFruits: true)]
let userArray2 = [User(userId: "1", email: "something@gmail.com", lovesFruits: false),
User(userId: "2", email: "something@gmail.com", lovesFruits: false)]
func areArraysEqual() -> Bool {
return userArray1 == userArray2
@jgsamudio
jgsamudio / ArrayEquatableExtension.swift
Created June 12, 2018 02:14
Array Extension For Equatable Protocol
extension Array: Equatable where Element: Equatable {
static func ==(lhs: Array<Element>, rhs: Array<Element>) -> Bool {
let count = lhs.count
  if count != rhs.count { return false }
for x in 0..<count {
  if lhs[x] != rhs[x] { return false }
}
return true
}
@jgsamudio
jgsamudio / RandomNumberGenerator4.1.swift
Created June 12, 2018 02:15
C import Random Number Generator
#if os(iOS) || os(tvOS) || os(watchOS) || os(macOS)
 return Int(arc4random())
#else
 return random() // or Int(rand())
#endif
let randomIntFrom0To10 = Int.random(in: 0 ..< 10)
let randomFloat = Float.random(in: 0 ..< 1)
@jgsamudio
jgsamudio / CssParser.swift
Last active July 12, 2018 02:41
A simple CSS to Swift Model Parser.
final class CssParser {
/// Parses the CSS file string into an array of CSS styles.
///
/// - Parameter fileContent: CSS file content.
/// - Returns: Array of CSS styles.
static func parse(fileContent: String) -> [CssStyle] {
var styles = [CssStyle]()
var pendingStyleName = ""
var pendingStyleProperties = [String: String]()