Skip to content

Instantly share code, notes, and snippets.

func open(_ filePath: String) throws -> NSArray {
let data = try Data(contentsOf: URL(fileURLWithPath: filePath))
let object: Any?
if #available(OSX 10.11, *) {
object = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data as NSData)
} else {
let unarchiver = NSKeyedUnarchiver(forReadingWith: data)
object = unarchiver.decodeObject(forKey: "root")
}
guard let unarchivedObject = object else {
@robertmryan
robertmryan / PixelProcessing.m
Last active May 24, 2021 08:28
Objective-C rendition of pixel processing found in http://stackoverflow.com/a/31661519/1271826
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:@"test.png"];
self.imageView.image = [self blackAndWhiteFromImage:image];
}
- (UIImage * _Nullable)makeBlackPixelsRedInImage:(UIImage *)image {
UInt32 black = [self pixelFromRed:0 green:0 blue:0 alpha:255];
UInt32 red = [self pixelFromRed:255 green:0 blue:0 alpha:255];
// I'd define a Swift struct that captures all of the needed `Complex` arithmetic:
/// Complex number
struct Complex {
let real: Double
let imaginary: Double
init(real: Double, imaginary: Double) {
self.real = real
import UIKit
import os.log
// This reported:
//
// 2016-10-19 21:27:58.383124 MyApp[6452:1422313] about to set to `nil`
// 2016-10-19 21:27:58.383439 MyApp[6452:1422313] set to nil
// 2016-10-19 21:27:58.383711 MyApp[6452:1422313] finished setting to `nil`
class ViewController: UIViewController {
// Swift 2
override func viewDidLoad() {
super.viewDidLoad()
perform1 {
self.perform2 {
self.perform3 {
print("everything done")
}
import UIKit
class ChildViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print("ChildViewController.viewDidLoad")
}
// Don't declare this `static`, as you never want to be tempted (or accidentally) refer to this static directly:
//
// static AFHTTPSessionManager *requestManager ;
@implementation Mysevers // this was the name you used
// always use this singleton
+ (AFHTTPSessionManager *)sharedHTTPSession {
static dispatch_once_t onceToken;
@robertmryan
robertmryan / ExportMPMediaItem.swift
Last active November 6, 2023 06:46
Export MPMediaItem to file in Documents
/// Export MPMediaItem to temporary file.
///
/// - Parameters:
/// - assetURL: The `assetURL` of the `MPMediaItem`.
/// - completionHandler: Closure to be called when the export is done. The parameters are a boolean `success`, the `URL` of the temporary file, and an optional `Error` if there was any problem. The parameters of the closure are:
///
/// - fileURL: The `URL` of the temporary file created for the exported results.
/// - error: The `Error`, if any, of the asynchronous export process.
func export(_ assetURL: URL, completionHandler: @escaping (_ fileURL: URL?, _ error: Error?) -> ()) {
import Cocoa
@IBDesignable
class HolyView: NSView {
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
let center1 = NSPoint(x: bounds.size.width * 0.3 + bounds.origin.x, y: bounds.size.height / 2 + bounds.origin.y)
let radius1 = bounds.size.width / 6
@robertmryan
robertmryan / GradientView.swift
Last active December 29, 2016 23:38
Cocoa IBDesignable gradient view
// GradientView.swift
//
// Created by Robert Ryan on 12/29/16.
// Copyright © 2016 Robert Ryan. All rights reserved.
//
import Cocoa
@IBDesignable
public class GradientView: NSView {