Skip to content

Instantly share code, notes, and snippets.

@staminajim
staminajim / LegacyInt2CodableExample.swift
Created April 26, 2019 06:22
A strategy for dealing with structs that have been made Codable in a Swift update, but clash with your existing implementation
/*
Example of a way to read in serialized data (eg. json) when Swift adds built in
Codable / Hashable / Equatible to a struct / class that previously didn't, yet they decided
to serialize things differently than you did in existing code.
In this example, I had previously serialized int2 to a dictionary with x and y keys for its
x and y properties. Swift 5 serializes this as an array, which is incompatible and causes
the whole json file to fail to deserialize.
You could write a custom init(from decoder: Decoder for each class using the problematic struct,
@artem-sherbachuk
artem-sherbachuk / FadingInOutAVPlayer.swift
Created January 19, 2017 09:43
Fading audio in and out AVPlayer
let player = AVPlayer()
func playFileAtURL(url: NSURL) {
let asset = AVAsset.assetWithURL(url) as AVAsset
let duration = asset.duration
let durationInSeconds = CMTimeGetSeconds(duration)
let item = AVPlayerItem(asset: asset)
let params = AVMutableAudioMixInputParameters(track: asset.tracks.first! as AVAssetTrack)