Skip to content

Instantly share code, notes, and snippets.

@lattner
lattner / async_swift_proposal.md
Last active April 21, 2024 09:43 — forked from oleganza/async_swift_proposal.md
Concrete proposal for async semantics in Swift

Async/Await for Swift

Introduction

Modern Cocoa development involves a lot of asynchronous programming using closures and completion handlers, but these APIs are hard to use. This gets particularly problematic when many asynchronous operations are used, error handling is required, or control flow between asynchronous calls gets complicated. This proposal describes a language extension to make this a lot more natural and less error prone.

This paper introduces a first class Coroutine model to Swift. Functions can opt into to being async, allowing the programmer to compose complex logic involving asynchronous operations, leaving the compiler in charge of producing the necessary closures and state machines to implement that logic.

@mbuchetics
mbuchetics / codeableEnum.swift
Created June 30, 2017 09:30 — forked from reckenrode/codeableEnum.swift
Implement Codable on an enum
struct User: Codable {
var name: String
var email: String
var id: String
var metadata: [String: MetadataType]
enum CodingKeys: String, CodingKey {
case name, email, id, metadata
}
}
@sbooth
sbooth / UnsafeMutablePointerTest.swift
Created November 3, 2016 12:06
Test the behavior of UnsafeMutablePointer.initialize(to:)
import Foundation
class Test {
init() {
print("init")
}
deinit {
print("deinit")
}
}
@algal
algal / PrintToStdErr.swift
Last active July 27, 2022 06:47
print to stderr in Swift 3
// known-good: Xcode 8, Swift 3
import Foundation
var standardError = FileHandle.standardError
extension FileHandle : TextOutputStream {
public func write(_ string: String) {
guard let data = string.data(using: .utf8) else { return }
@jspahrsummers
jspahrsummers / bad.m
Last active January 20, 2021 11:55
Synchronizing with multiple GCD queues
//
// DON'T do this, or else you risk a deadlock (e.g., by accidentally performing it in a different order somewhere)
//
dispatch_async(firstQueue, ^{
dispatch_sync(secondQueue, ^{
// code requiring both queues
});
});
@dabrahams
dabrahams / Properties.md
Last active November 7, 2017 08:29
Status of Swift property guidelines discussion as of 2016-01-27

Properties

Conclusions After Much Discussion

  • Things that produce a logical transformation of the whole value are methods. That includes transformed views such as the result of reverse() on a bidirectional collection.

  • Things that mutate state (even state outside the receiver) are methods.

import Foundation
import IOKit
import IOKit.usb
import IOKit.usb.IOUSBLib
print("Scanning USB Bus.....\n\n\n")
//
#import <Foundation/Foundation.h>
@protocol OS_Foo <NSObject> @end
typedef NSObject<OS_Foo> * Foo;
@protocol OS_Bar <NSObject> @end
typedef NSObject<OS_Bar> *__attribute__((objc_independent_class)) Bar;
@interface Test : NSObject
@end
@broomburgo
broomburgo / Optional.h
Last active September 5, 2018 08:21
Optional type for Objective-C
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface Optional : NSObject
+ (Optional*)with:(id _Nullable)value;
+ (Optional*)with:(id _Nullable)value as:(Class _Nonnull)valueClass;
@chriseidhof
chriseidhof / json.swift
Last active March 21, 2019 07:45
Reflection
import Cocoa
struct Person {
var name: String = "John"
var age: Int = 50
var dutch: Bool = false
var address: Address? = Address(street: "Market St.")
}
struct Address {