Skip to content

Instantly share code, notes, and snippets.

View eofster's full-sized avatar

Alexey Kuznetsov eofster

View GitHub Profile
private func audioBufferListWithObjectID(objectID: AudioObjectID, scope: AudioObjectPropertyScope) throws -> AudioBufferList {
var address = audioBufferListAddressWithScope(scope)
var length = try propertyDataSizeWithObjectID(objectID, address: address)
let bytes = UnsafeMutablePointer<AudioBufferList>.alloc(Int(length))
defer { bytes.destroy() }
let status = AudioObjectGetPropertyData(objectID, &address, 0, nil, &length, bytes)
if status != noErr {
throw TelephoneError.SystemAudioDevicePropertyDataGetError(systemErrorCode: Int(status))
}
return bytes.memory
@eofster
eofster / ObserverPattern.swift
Last active February 1, 2016 17:20
An example of using the observer pattern instead of NSnotifications and KVO
class UserAgent {
let observer: UserAgentObserver
init(observer: UserAgentObserver) {
self.observer = observer
}
func start() {
observer.userAgentDidFinishStarting(self)
@eofster
eofster / CompositeObserver.swift
Last active November 8, 2015 14:42
An example of using a composite observer
class UserAgent {
let observer: UserAgentObserver
init(observer: UserAgentObserver) {
self.observer = observer
}
func start() {
observer.userAgentDidFinishStarting(self)
@eofster
eofster / PhoneCallRegistryOptionalPhoneCall.swift
Last active November 10, 2015 11:26
Phone call registry returning an optional phone call
protocol PhoneCallRegistry {
func callWithIdentifier(identifier: Int) -> PhoneCall?
}
func hangUpCallWithIdentifier(identifier: Int) {
if let call = phoneCallRegistry.callWithIdentifier(identifier) {
call.hangUp()
} else {
print("Could not hang up call")
}
@eofster
eofster / PhoneCallRegistryNonOptionalPhoneCall.swift
Created November 10, 2015 11:01
Phone call registry returning non-optional phone call
protocol PhoneCallRegistry {
func callWithIdentifier(identifier: Int) -> PhoneCall
}
func hangUpCallWithIdentifier(identifier: Int) {
phoneCallRegistry.callWithIdentifier(identifier).hangUp()
}
@eofster
eofster / AccountPhoneCallPyramidOfDoom.swift
Last active November 10, 2015 11:29
Pyramid of Doom when accessing first call of an account with identifier
func hangUpFirstCallOnAccountWithIdentifier(identifier: Int) {
if let account = accountRegistry.accountWithIdentifier(identifier) {
if let call = account.firstCall {
call.hangUp()
} else {
print("Could not find first call for an account")
}
} else {
print("Could not find account")
}
@eofster
eofster / AccountPhoneCallNullObject.swift
Created November 10, 2015 11:23
Usage of the Null Object pattern when hanging up call on account with identifier
func hangUpFirstCallOnAccountWithIdentifier(identifier: Int) {
accountRegistry.accountWithIdentifier(identifier).firstCall.hangUp()
}
@eofster
eofster / NullObjectReferenceTypeInheritance.swift
Last active November 10, 2015 13:11
An example implementation of Null Object pattern for reference types using inheritance
class PhoneCall {
static let nullPhoneCall: PhoneCall = NullPhoneCall()
let identifier: Int
init(identifier: Int) {
self.identifier = identifier
}
@eofster
eofster / NullObjectReferenceTypeProtocol.swift
Last active August 3, 2017 16:17
An example implementation of Null Object pattern for reference types using protocol
protocol PhoneCall {
var identifier: Int { get }
var isNil: Bool { get }
func hangUp()
}
class RealPhoneCall: PhoneCall {
let identifier: Int
let isNil = false
@eofster
eofster / AccountPhoneCallGuard.swift
Created November 10, 2015 12:20
Guard statements when accessing first call of an account with identifier
func hangUpFirstCallOnAccountWithIdentifier(identifier: Int) {
guard let account = accountRegistry.accountWithIdentifier(identifier) else {
print("Could not find account")
return
}
guard let call = account.firstCall else {
print("Could not find first call for an account")
return
}
call.hangUp()