Find the right value in an Array, or in an Array of Dictionaries

From time to time I discover a new way of coding something that significantly improves readability & minimizes how much I need to code.  Here’s one of such latest discoveries. Typically to find a value in an array (or in an array of dictionaries) with a specific condition I would do a for loop with …

Inserting cells at the top of a UITableView with no scrolling

If you would like your table to be refreshed “ala Twitter”, with new data being added on top, here’s a quick an easy way to do it for iOS:  func updateTableViewAfterAdditionInNewArray(){         //Get new feeds and check if counts are different         let countOfAddedItems = oldArray.count – newArray.count    …

Multi Thread access to shared Variables – Swift 4 Concurrency

Working with background threads really helps improve the app responsiveness & the overall user experience. But, and it’s a big but, you have to be aware of multithread access conflicts.  If you access/modify the same variable from multiple threads you’re prone to non-reproducible -seemingly random- crashes. How to Detect Conflicts? To help you find where …

Random numbers in Swift

Swift 4.2 has introduced a native random number API in the standard library, making it cross platform.  Int.random(in: 1…1000) // → 691 Double.random(in: 0..<1) // → 0.8741555749903935 UInt32.random(in: 0xD800…0xDFFF) // → 55666 Read more in the great article from Ole Begemann, worth a read.    Marc

Resize NSImage Proportionally (Swift)

I wanted a simple function to resize an image to a desired height, while keeping proportions. Here’s what I came up with (based on Marco implementation): extension NSImage {          func resizedImageTo(sourceImage: NSImage, newSize: NSSize) -> NSImage?{         if sourceImage.isValid == false {             …

NSWindow min and max size (Swift 4)

You can force the views to keep a inferred min and max width/height by using AutoLayout, but what if you’re working with several views simultaneously? Wouldn’t it be simpler to have a window min and max width/height?  Just add the following in your NSWindowController: override func windowDidLoad() {         super.windowDidLoad()     …

Open links in the Background

Here’s a quick tip on how to open a URL in the background or foreground depending on a user-set preference.  if(openInBackgroundPreferenceSet == true){                  NSWorkspace.shared.open([linkToOpen], withAppBundleIdentifier: nil, options: NSWorkspace.LaunchOptions.withoutActivation, additionalEventParamDescriptor: nil, launchIdentifiers: nil) }else{                 NSWorkspace.shared.open(linkToOpen) }   Questions / comments? I’m at @MarcMasVi  …