Skip to content

Instantly share code, notes, and snippets.

View PetreVane's full-sized avatar
🍃

Petre Vane PetreVane

🍃
View GitHub Profile
// Decoding User Info
func decodeUserInfo(from data: Data) -> Result<User, Failure> {
let decoder = JSONDecoder()
let decodedData = try? decoder.decode(DecodedUserInfo.self, from: data)
guard let userData = decodedData?.person else { print("Errors while decoding UserData: \(Failure.failedDecodingFile.errorDescription)"); return .failure(.failedDecodingFile)}
let user = User(userID: userData.id, userName: userData.username.content, iconServer: userData.iconserver, iconFarm: userData.iconfarm)
_ = user.iconURL
@PetreVane
PetreVane / Retreive UserInfo from FileManager.swift
Last active January 24, 2020 17:25
Remember to save UserInfo to fileManager, before attempting to retrieve any data
func retrieveUserInfo() throws -> User {
let plistDecoder = PropertyListDecoder()
let filePath = FileManager.documentsDirectory
let file = filePath.appendingPathComponent("SavedUserData").appendingPathExtension("plist")
guard let retrievedData = try? Data(contentsOf: file) else { print("Errors: \(Failure.missingFile.errorDescription)"); throw Failure.missingFile }
guard let decodedUserData = try? plistDecoder.decode(User.self, from: retrievedData) else { print("Error: \(Failure.failedDecodingFile.errorDescription)"); throw Failure.failedDecodingFile}
@PetreVane
PetreVane / Fragment
Last active January 24, 2020 17:24
In order to do any kind of zooming, you need to implement a Scrollviewdelegate Protocol. This protocol contains a bunch of handy methods. The method we're after though, is viewForZooming (in: UIScrollView), which just sets the view to be zoomed. All you need to do is to return the view that you want to zoom from this method. If there are multip…
extension PhotoDetailsVC: UIScrollViewDelegate {
/// Returns the view that is expected to be zoomed
/// - Parameter scrollView: A view that allows the scrolling and zooming of its contained views.
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageView
}
@PetreVane
PetreVane / ResizeImage.swift
Last active January 24, 2020 17:23
Helper method which scales down the size of an image, before being uploaded to Firebase Storage
extension UIImage {
// Usage:
// let scaledImage = image.scale(newWidth: 960.0)
func reduceImageSize(to newWidth: CGFloat) -> UIImage {
if self.size.width == newWidth {
return self
}
// Calculates the scaling factor
@PetreVane
PetreVane / Converts Unix time in Date.swift
Created November 18, 2019 14:27
Decodes Unix time in Time that has a significance
func decodeDatefrom(_ unixTime: String) -> String? {
// converts comment date into meaningful date
var decodedDate: String?
if let unixTime = Double(unixTime) {
let dateFormatter = DateFormatter()
let date = Date(timeIntervalSince1970: unixTime)
@PetreVane
PetreVane / Lazy Closures.swift
Last active January 24, 2020 17:22
"Yes, you often need to explicitly declare the type. Yes, you need that = sign. And Yes, you need the parentheses after the closing brace."
// Lazy Closures
class Person {
let name: String
init(name: String) {
self.name = name
}
// Remember to add the closing brace '()' at the end of the closure declaration
@PetreVane
PetreVane / Lazy Methods.swift
Last active January 24, 2020 17:20
Remember to mark your method as 'Private' so you don't call it by accident
// Lazy methods
class Person {
let name: String
init(name: String) {
self.name = name
class MusicPlayer {
init() {
print("Music Player has started")
}
}
class Singer {
@PetreVane
PetreVane / Iterate over resources with FileManager.swift
Created December 10, 2019 12:39
Iterates over project resources, by getting a Bundle.main refeference
class Testing: UIViewController {
let fileManager = FileManager.default
override func viewDidLoad() {
super.viewDidLoad()
if let path = Bundle.main.resourcePath {
guard let items = try? fileManager.contentsOfDirectory(atPath: path) else { print("Path is not available"); return }
for item in items {
@PetreVane
PetreVane / Navigation Bar tricks.swift
Last active January 24, 2020 17:18
Hides the Navigation Bars on tap
override func viewWillAppear(_ animated: Bool) {
uper.viewWillAppear(animated)
navigationController?.hidesBarsOnTap = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.hidesBarsOnTap = false
}