Skip to main content

Posts

Showing posts from June, 2015

Similarly different: join(), reduce() and flatMap() in Swift 2

This is the first ever piece that I've written first as a playground and second as a blogpost. This is something that I'd like to make the norm wherever possible and will endeavour to do so. It covers a selection of higher order functions in Swift and draws attention to their similarities and differences. Enjoy! Similarly different: join(), reduce() and flatMap() in Swift 2 reduce(), flatMap() and join() can produce the same results let nestedArray = [[1,2,3,4],[6,7,8,9]] let joined = [].join(nestedArray) let flattened = nestedArray.flatMap{$0} let reduced = nestedArray.reduce([], combine: {$0 + $1}) joined // [1, 2, 3, 4, 6, 7, 8, 9] flattened // [1, 2, 3, 4, 6, 7, 8, 9] reduced // [1, 2, 3, 4, 6, 7, 8, 9] The differences only real become clear when we want to add elements to the array we're flattening let joinedPlus = [5].join(nestedArray) let flattenedPlus = nestedArray.flatMap{$0 + [5]} let reducedPlus = nestedArray.reduce([], combine: {$0 + [5] + $1}) joine

Fear and Loathing in Auto Layout: Constraint Multipliers, Ratios, Constants and Relations (Xcode 7 beta 2)

Multipliers and Ratios Up until now the Auto Layout series of posts has been using constraints with constant widths and heights, but there is another way. This involves using multipliers, ratios and relations. In this post, I'll show you in practical terms what this means for your layout. 1.  To begin, add a single button to the left corner of your view. 2.  Connect it to the left and bottom sides of the superview by Ctrl + dragging, and selecting the top option in the list that appears each time you let go of the drag (as we've done many times before in the previous posts). 3.  Drag out a constraint to the right of the superview in the same way as point 2. 4.  Double click the final constraint (connecting the button to the right of the superview) and change the value in the Constant field to zero. With the Constant set to zero and the Multiplier set to 1, 100% or 1:1 the button will stretch the entire width of the screen. 5.  Now double click on the constr

Empowering Extensions in Swift 2: Protocols, Types and Subclasses (Xcode 7 beta 2; updated Swift 3, Xcode 8)

Type extensions with generic type parameters Extensions are a convenient way of separating code, extending pre-existing types and adopting new protocols in Swift. But with the arrival of Swift 2 they've taken on a whole new set of powers. I've already looked briefly at the extension of protocols  (in Xcode 7 beta 1), but with the arrival of Xcode 7 beta 2 we can now perform even more magic. First up Consider this extension: extension Array where T:UIView { var backgroundColors:[UIColor] { return self.filter{$0.backgroundColor != nil}.map{$0.backgroundColor!} } } Swift 3, Xcode 8 extension Array where Element:UIView { var backgroundColors:[UIColor] { return self.filter{$0.backgroundColor != nil}.map{$0.backgroundColor!} } } It extends arrays with a type of UIView to add a computed variable that is capable of returning all the colours used inside an array of views. Tipping the scale A second example here enables an array of views t

Fear and Loathing in Auto Layout: Content Hugging Priority (Xcode 7 beta 2)

Content Hugging: A Brief Embrace At the end of yesterday's post we hovered over the input boxes for Content Compression Resistance Priority and discovered that in short it simply means 'Resistance to being made smaller'. And from hovering over the equivalent input boxes for Content Hugging Priority (CHP) we see that Hugging defines the resistance to being made larger. It is this "resistance to being made larger", or hugging, that I want now to briefly look at in a rather contrived example here: 1.  You can either begin with the same project as last time and remove the spacer view from your view controller, or you can drag out two buttons and connect restraints to the edges closest the superview and to the bottom of the superview before proceeding. 2.  Once you've completed step 1, add a background colour to the buttons that remain. 3.  Stretch the leftmost button so that it almost touches the rightmost one and then connect the two butto

Fear and Loathing in Auto Layout: Content Compression Resistance Priority and Constraint Priority (Xcode 7 beta 2)

Content Compression Resistance: Stop Making me Feel Small! This post is one of a pair of posts looking at compression resistance and hugging priority. It follows yesterday's post on size classes in auto layout . As before this is a very practical example, and you will benefit by following along in your own project as you read. 1. First of all drag out two buttons. Placing one on the left bottom corner of the screen and the other in the bottom right corner. 2. Between them place an empty view (for which you might want to change the background colour for the sake of visibility). 3. Fix the height of the central view using the third icon along the bottom right of the storyboard pane, and adding the constraint. 4. Drag out constraints between the left button and the left side and the bottom of the superview and for the right button drag out constraints to the right side and to the bottom of the superview. 5. Connect the central view horizontally to the right button an

Fear and Loathing in Auto Layout: Playing Hide and Seek with Class Sizes (Xcode 7 beta 2)

The time has come to write about size classes in Auto Layout . This is something that previous posts have been leading up to and I think we're now ready to address this. Alternative views 1.  Place a view as usual in a view controller leaving some space at the bottom for another view. 2.  Drag out constraints to make sure it resizes proportionally by linking it to each edge of the superview. (Note: If you're unsure how to do this refer to  earlier posts .) 3.  Now click on the words wAny hAny  at the bottom centre of the storyboard pane. 4.  A popover should appear with a grid inside.  5.  Drag over the grid until two squares across are covered and three down, and at the bottom of the popover the text reads Base Values: For All Regular Height Layouts . Click inside the blue area of the grid. 6.  Drag out a second view and place it below the first. 7.  Drag out constraints from the new view to the three superview edges (bottom, left and right

Functional Programming: Understanding the Beauty of Custom Operators in Swift

I confess that as soon as talk turns to custom operators, especially ones of three characters in length, my eyes glaze over. Well that is how things were until yesterday. Yesterday things started to become clear when I learnt (from  Functional Programming in Swift ) that these operators aren't fancy ways of hiding code to increase its opacity. They in fact increase the transparency of code. Once you understand what they do! Are you sitting comfortably? I'm going to begin with a practical example. Let's suppose we want the ability to add various styles to an NSAttributedString. As in the previous posts on functional programming we start with a typealias to define the function we'd like to return. (Note: This starting point is a staple part of the approach demonstrated in Eidhof et al.'s book.) typealias Attributer = NSAttributedString -> NSAttributedString It takes an NSAttributedString and returns an NSAttributedString. Now it's time to write t

Fear and Loathing in Auto Layout: Side by Side (Xcode 7 beta 2)

So far this series of posts has examined the scenario where a view is on its own and a view is within a view . Now it examines placing two views side by side. I want to start with the scenario of a pair of views that remain equal in width and height. Both views remain equal widths and heights 1.  Place two views side by side 2.  For each view, control + click and drag (or three finger drag) out constraints for three of the sides, excluding the edge closest to the view next to it. Letting go of the drag each time and select the top option each time. (these will read: Vertical Spacing to Top Layout Guide, Leading Space to Container Margin, Trailing Space to Container Margin, Vertical Spacing to Top Layout Guide) 3.  Now drag out a constraint between the two views and select the top option once again (i.e. Horizontal Spacing) 4.  Next select both views at the same time 5.  Click on the third icon along the row of bottom right icons in the storyboard pane 6