Skip to content

Instantly share code, notes, and snippets.

View PetreVane's full-sized avatar
🍃

Petre Vane PetreVane

🍃
View GitHub Profile
@PetreVane
PetreVane / URLSession.dataTask example.swift
Created December 11, 2019 12:37
Simple URLSession Data Task with default Session Configuration
import UIKit
import PlaygroundSupport
// Declare a session configuration
let defaultConfiguration = URLSessionConfiguration.default
defaultConfiguration.allowsCellularAccess = true
defaultConfiguration.waitsForConnectivity = true
// assign that configuration to a session
let session = URLSession(configuration: defaultConfiguration)
@PetreVane
PetreVane / URLSession.dataTask used when Uploading.swift
Last active February 5, 2020 13:14
Upload some dummy data with URLSession.dataTask
import UIKit
// get some dummy json
let json = "{'name' : 'john snow'; 'age' : '29'}"
// get a reference to upload server
let url = URL(string: "https://httpbin.org/post")!
var request = URLRequest(url: url)
@PetreVane
PetreVane / FlyweightPattern.swift
Last active January 24, 2020 17:17
The Flyweight Pattern is a structural design pattern that minimizes memory usage and processing. The flyweight pattern provides a shared instance that allows other instances to be created too. Every reference to the class refers to the same underlying instance.
import UIKit
let red = UIColor.red
let red2 = UIColor.red
print(red === red2) // proves this is a flyweight
let color = UIColor(red: 1, green: 0, blue: 0, alpha: 1)
let color2 = UIColor(red: 1, green: 0, blue: 0, alpha: 1)
print(color === color2) // proves this is NOT a flyweight
@PetreVane
PetreVane / Navigation: Scene Delegate sample.swift
Created January 3, 2020 16:25
This is a sample of Scene delegate methods, showing the basics of navigation in a project without StoryBoard. There are 2 ViewController files in the project: SearchBiewController & FavoritesViewController; each of them is embeded in a Navigation Controller
import UIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
@PetreVane
PetreVane / Add TapGesture Recognizer.swift
Created January 7, 2020 15:44
This methods adds a gesture recognizer on your current view, which dissmises the keyboard when tapped away
func dismissKeyboardGesture() {
let tapGesture = UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:)))
view.addGestureRecognizer(tapGesture)
}
@PetreVane
PetreVane / Convert Date To Month-Year Format.swift
Created February 2, 2020 15:22
These 2 extensions convert a String to Date, and back to String
extension Date {
//2
func convertToMonthYearFormat() -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM yyyy"
return dateFormatter.string(from: self)
}
}
@PetreVane
PetreVane / CAShapeLayer Shapes.swift
Created April 1, 2020 09:25
Creates rectangles, triangles and ovals
extension CAShapeLayer {
static func rectangle(roundedRect: CGRect, cornorRadius: CGFloat, color: UIColor) -> CAShapeLayer {
let path = UIBezierPath(roundedRect: roundedRect, cornerRadius: cornorRadius)
let shape = CAShapeLayer()
shape.path = path.cgPath
shape.fillColor = color.cgColor
return shape
}
}
@PetreVane
PetreVane / Stepper.swift
Created June 20, 2020 19:57
How to initialize a Stepper in SwiftUI
import SwiftUI
struct ContentView: View {
@State private var orderCount = 0
var body: some View {
VStack {
// Initializing a Stepper
@PetreVane
PetreVane / Toggle and DatePicker.swift
Created June 20, 2020 19:58
Adding a Toggle and a DatePicker in SwiftUI
import SwiftUI
struct ContentView: View {
private var isActivatedMessage: String {
return "CatNip mode is " + (isActivated ? "Activated" : "Deactivated")
}
private var dateFormater: DateFormatter {
let dateFormater = DateFormatter()
@PetreVane
PetreVane / StarWars text like.swift
Created June 20, 2020 19:58
How to add StarWars text like and heart that changes size and color when pressed
import SwiftUI
struct ContentView: View {
@State private var colorChange = false
@State private var sizeChange = false
var body: some View {
VStack(alignment: .center, spacing: 30) {