Closure in Swift: A reminder on how awesome they are.

I was re-reading Apple’s documentation about closure and I tough it couldn’t hurt anybody to share this little resume about closure syntax.

Here’s a simple closure :

Here’s the same but simpler :

Hmm even simpler :

Huh ?!? Even more :

WTF ?!?! Really ?! (but yeah it works) :

And finally (unreadable but still) :

From The Swift Programming Language (Swift 3.0.1)

Cheers,

MAB

UIViewController extension for child view controller containment.

Hello !  I know its been a while, holidays and all …

Here’s a little extension that does no magic but shortens your code if you have to load child view controller from storyboards.  Sorry for the lack of syntax colouring, WordPress does not seem to support Swift yet.

[code language=”text”]
import Foundation

typealias ViewControllerStoryboardInfo = (name: String, id: String?)

protocol Container {
}

extension Container where Self: UIViewController {

func addChildViewController(withInfo info: ViewControllerStoryboardInfo)-> UIViewController? {

var viewController: UIViewController! = nil

switch info.id {
case let identifier where identifier != nil:
viewController = UIStoryboard(name: info.name, bundle: nil).instantiateViewController(withIdentifier: identifier!)
default:
viewController = UIStoryboard(name: info.name, bundle: nil).instantiateInitialViewController()
}

if let viewController = viewController {

self.addChildViewController(viewController)
viewController.view.frame = self.view.bounds
self.view.addSubview(viewController.view)
viewController.didMove(toParentViewController: self)
return viewController
}

return nil
}

func removeAllChildViewController() {
self.childViewControllers.forEach({ (controller) in
controller.view.removeFromSuperview()
controller.willMove(toParentViewController: nil)
controller.removeFromParentViewController()
})
}
}
[/code]

 

Cheers,

MAB

Zeplin.io

It’s free pub for them but they certainly deserve it !  All of you dev out there you need to try this.  Pick you project PSD or Sketch, import it and enjoy.

  • Easy asset integration
  • Code generation for UIColors and UIFont extensions used in the PSD
  • Measurements for everything in point ‘pt’
  • Comments on UI elements between users

Downside … a little bit expensive in my humble opinion.

Zeplin.io

screen-shot-2016-12-14-at-9-10-42-am

 

Xcode server and Cocoapods : Your local changes to the following files would be overwritten by merge.

Hi all,

Sorry for the delay, I have been busy building up a Swift 3 framework for OpenGL and Vuforia.

Back to our main topic.  I recently built an Xcode server for my project.  The project uses Cocoapods and for a time it was good then, a few days ago, a build exited with the error :

Your local changes to the following files would be overwritten by merge.

And has been failing ever since until I found the solution.  It seems the cocoa pods repo gets messed up sometime and you need to hard reset it to HEAD.  Here how :

cd ~/.cocoapods/repos/master

git reset HEAD –hard

Takes a couple of minutes and then you should be just fine !

Enjoy 🙂

MAB

Bring you app to life – A very simple guide to animated buttons and backgrounds

Hey !

I know its been a while, I am qui busy at Moment Factory right now.  Those guys are geniuses.  Anyway, I wanted to share with you, if you don’t already know it, how to animate anything that uses a UIImage aka. UIImageView, UIButton, etc…

To create an animated UIImage, include in you project a folder that contains your animation with a PNG for each frame.  The files needs to be named like this:

  • WhatEverNameYouWant-1
  • WhatEverNameYouWant-2
  • WhatEverNameYouWant-3
  • WhatEverNameYouWant-4

I strongly recommend not to include these in an asset file, if you try you’ll quickly understand why.

Then you create a UIImage :

You can the use it with a UIButton like you would with any other UIImage :

That’s it !  Have fun animating you apps !!

MAB

Dispatch After

Just because I can never remember the correct syntax for a dispatch_after I made this very simple  wrapping function that I want to share with you.

I feel like it should have always been this way …

Make your custom view’s properties available in Storyboards

Let’s say you want to configure some parts of your view rendering directly in your storyboards.  Fortunately you can by using @IBInspectable.

Syntax

Result in Xcode

Screen Shot 2016-06-14 at 3.34.17 PM

Simulator

Screen Shot 2016-06-14 at 3.34.13 PM

Here is the project ( Xcode 8, Swift 3) if you want to try it : IBInspectableDemo

That’s it folks, have a good day !

MAB