Skip to content

Instantly share code, notes, and snippets.

@DominikPetho
DominikPetho / .swift
Last active September 27, 2018 15:14
Class-Sample
class Person {
var name: String
var surname: String
init(name: String, surname: String) {
self.name = name
self.surname = surname
}
@DominikPetho
DominikPetho / .swift
Last active September 27, 2018 15:17
Struct-sample
//Init is automatically created
struct Person {
var name: String
var surname: String
}
let person = Person(name: "Dante", surname: "Brazeal")
//Assign person to another variable.
var secondPerson = person
//Change name of second person
@DominikPetho
DominikPetho / SimpleViewHelper.swift
Created October 1, 2018 11:02
Simple types used in SimpleViewController
// Define protocol
protocol SimpleViewCellItem {}
// Let all types, which you want to show in table view, comforms to that protocol
struct Person: SimpleViewCellItem {
var name: String
var surname: String
}
@DominikPetho
DominikPetho / SimpleViewController.swift
Last active October 1, 2018 13:03
Main implementation of SimpleViewController
public final class SimpleViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
// List of items, showed in TableView
var peopleWithAdvertisementList: [SimpleViewCellItem]!
public override func viewDidLoad() {
setupTableView()
createDummyList()
@DominikPetho
DominikPetho / .swift
Created October 1, 2018 11:11
Disatvantage method
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var dequeuedCell: UITableViewCell?
if peopleWithAdvertisementList[indexPath.row] is Advertisement {
dequeuedCell = tableView.dequeueReusableCell(withIdentifier: "AdvertisementCell")
} else if peopleWithAdvertisementList[indexPath.row] is Person {
dequeuedCell = tableView.dequeueReusableCell(withIdentifier: "PeopleCell")
} else {
// Else what? Do we have other type comforming to SimpleViewCellItem protocol?
}
@DominikPetho
DominikPetho / .swift
Created October 1, 2018 11:32
Simple CellType
extension SimpleViewController {
enum CellType {
case advertisement(Advertisement)
case person(Person)
}
}
@DominikPetho
DominikPetho / .swift
Last active October 1, 2018 13:03
SimpleViewController using enum
// Right now you can delete SimpleViewCellType protocol
struct Person {
var name: String
var surname: String
}
struct Advertisement {
var brandName: String
}
@DominikPetho
DominikPetho / .swift
Created October 10, 2018 05:08
Simple Controller with outlets
class ViewController: UIViewController {
@IBOutlet weak var userImageView: UIImageView!
@IBOutlet weak var messageButton: UIButton!
@IBOutlet weak var followButton: UIButton!
@IBOutlet weak var userNameLabel: UILabel!
@IBOutlet weak var phoneNameButton: UIButton!
@IBOutlet weak var phoneNameLabel: UILabel!
@IBOutlet weak var phoneNameRightImageView: UIImageView!
struct User {
let name: String
let surname: String
let photo: UIImage
}
class ViewControllerHeaderView: UIView {
@IBOutlet weak var userImageView: UIImageView!
@IBOutlet weak var messageButton: UIButton!
@DominikPetho
DominikPetho / .swift
Last active September 15, 2020 07:36
Sample of Swift property wrappers for unit testing
import UIKit
@propertyWrapper
struct Printer<T> {
public var currentValue: T?
var wrappedValue: T? {
get {
print("Property wrapper value printed", currentValue)