Skip to content

Instantly share code, notes, and snippets.

View jorgenisaksson's full-sized avatar

Jörgen Isaksson jorgenisaksson

View GitHub Profile
@ccwasden
ccwasden / WithPopover.swift
Created February 5, 2020 13:39
Custom size popover in iOS SwiftUI
// -- Usage
struct Content: View {
@State var open = false
@State var popoverSize = CGSize(width: 300, height: 300)
var body: some View {
WithPopover(
showPopover: $open,
popoverSize: popoverSize,
@stinger
stinger / CombineFetcherAndDecoder.swift
Last active February 17, 2024 02:07
Combine - fetching and decoding JSON data
import Foundation
import Combine
enum APIError: Error, LocalizedError {
case unknown, apiError(reason: String), parserError(reason: String)
var errorDescription: String? {
switch self {
case .unknown:
return "Unknown error"
@caroillemann
caroillemann / Root.plist
Last active April 6, 2021 02:50 — forked from hujunfeng/Root.plist
Add version in Settings.bundle for iOS apps
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>StringsTable</key>
<string>Root</string>
<key>PreferenceSpecifiers</key>
<array>
<dict>
<key>Type</key>
@shaps80
shaps80 / 1. UserDefaults+Key.swift
Last active September 25, 2020 08:29
Adds a Swift extension to make UserDefaults more consistent to work with.
//
// UserDefaults.swift
//
// Created by Shaps Benkau on 24/05/2018.
// Copyright © 2018 152percent Ltd. All rights reserved.
//
import Foundation
#if os(iOS)
@beccadax
beccadax / JSONFeed.swift
Last active June 18, 2020 08:21
JSON Feed parser in Swift 4—now updated for Xcode 9 Beta 1. Uses the new Codable protocol and JSONDecoder class.
import Foundation
let feedURL = URL(string: "https://daringfireball.net/feeds/json")!
// MARK: Basic models
// We do not model the `userComment` field because it is not supposed to be used by
// machines. We do not model extensions because they should be ignored unless
// supported anyway.
struct Feed {
@kristopherjohnson
kristopherjohnson / MyInitiallyPositionedWindowController.swift
Created December 8, 2015 16:48
Snippet to programmatically set initial position of an Cocoa window
import Cocoa
class MyInitiallyPositionedWindowController: NSWindowController {
override func windowDidLoad() {
super.windowDidLoad()
if let window = window, screen = window.screen {
let screenRect = screen.visibleFrame
let offsetFromLeft = CGFloat(200)
@NatashaTheRobot
NatashaTheRobot / WatchConnectivitySingletonDemo.swift
Last active May 21, 2024 18:21
WatchConnectivity Singleton Demo
//
// WatchSessionManager.swift
// WatchConnectivityDemo
//
// Created by Natasha Murashev on 9/3/15.
// Copyright © 2015 NatashaTheRobot. All rights reserved.
//
import WatchConnectivity
@jorgenisaksson
jorgenisaksson / gist:76a8dae54fd3dc4e31c2
Created June 10, 2014 18:01
Create a CGPath from an NSBezierPath in Swift. Great for CALayers for example.
// Adapted from Cocoa Drawing Guide's "Create a CGPathRef fram an NSBezierPath Object"
func CGPathFromNSBezierPath(nsPath: NSBezierPath) -> CGPath! {
if nsPath.elementCount == 0 {
return nil
}
let path = CGPathCreateMutable()
var didClosePath = false
@nielsbot
nielsbot / gist:5294660
Last active December 15, 2015 17:09
Make sure your layer is pixel-aligned when setting it's position. (Avoids things looking blurry)
// when setting the position of a layer, use this:
// layer.position = [ layer pixelAlignedPostionForPoint:<originalPoint> ] ;
@implementation CALayer (SetPositionPixelAligned)
-(CGPoint)pixelAlignedPositionForPoint:(CGPoint)p
{
CGSize size = self.bounds.size ;
CGPoint anchorPoint = self.anchorPoint ;