Pattern matching in Swift
One of the best explanations about pattern matching in swift I've seen so far. ==Insta-bookmark.==
One of the best explanations about pattern matching in swift I've seen so far. ==Insta-bookmark.==
Even though the names might sound self-explanatory, I never really understood how they work. But I finally found a proper explanation about them:
A strong hugging priority limits the view from growing much larger than the content it presents. A weak priority may allow a view to stretch and isolate its content among a sea of padding. Compression resistance describes how a view attempts to maintain its minimum intrinsic content size.
I'm considering to buy the book too, while I'm at it.
Update, 2015-09-16: This piece is really, really nice as well. As he points out:
When I first started out, I actually wish I could have had something like this guide to help me out.
I recently saw this medium post linked on The Loop, and at first I thought "sure, this is a major issue, but, on the other hand, it seems pretty fair to be paid by the number of plays, that's what streaming means; I can't see any another way", but eventually I reached the part where he offers a solution; and I think it's perfect:
This was a fun Saturday morning read. And ==very== interesting.
Continuing where we left off last time, is another little trick I like to use with enums:
func collectionView(_ collectionView: UICollectionView, layout: UICollectionViewLayout, referenceSizeForFooterInSection section: NSInteger) -> CGSize {
if .products == Section(rawValue: section), conditionOne, !conditionTwo {
return CGSize(width: 10, height: 20)
}
else if .shippingDetails == Section(rawValue: section), anotherCondition {
return CGSize(width: 10, height: 40)
}
return .zero
}I recently wrote that I added a visual representation for when a search is performed, by autocompleting the search field with the search query. Everything appeared to work as expected.
Some time ago I added Tinfoil Heroku addon and never saw any security problems. I even thought I will never see one. But I started seeing a couple about Cross-Site Scripting in HTML tag. I went to their dashboard, clicked on reproduce the attack, it took me to my website, with a search performed, the search query was autocompleted, as expected, but a tag was also injected in the search field:
This will only work for enums that are Ints, start from 0, and increment by 1 - for example tableView / collectionView sections, but I think it's a nice little trick that I always use:
private enum Section: Int {
case products
case shippingDetails
case paymentDetails
case totalValue
case numberOfSections
}Now you can do the following:
func numberOfSections(in collectionView: UICollectionView) -> Int {
return Section.numberOfSections.rawValue
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: NSIndexPath) -> UICollectionViewCell {
let cell: UICollectionViewCellI recently saw a nice example of Mixins for SASS. So I went ahead and created a few, since I really dig them:
@mixin responsive_width($width) {
@media screen and (max-width: $width) { @content; }
}
@include responsive_width(475px) {
// Custom styles for width <= 475px
}
@mixin light_bottom_border($size) {
border-bottom: $size solid $light_gray;
}
blockquote {
@include light_bottom_border(5px);
@include light_top_border(5px);
}
article {
@include light_bottom_border(1px);
}I also unified most of the website's colors with variables like $link_color, $gray_color and $text_color. Maybe one day I will try to unify the paddings and margins as well.
This was fun, and surprisingly satisfying.
I wanted a visual representation and helper for the user when a search is performed on the site. So I thought about autocompleting the searched term in the search field. At first I tried with jQuery:
var query = decodeURIComponent(location.search)
.split("=")[1]
.replace(/[\+]/g, " ");
$('input.search').val(query);
$('input.banner-search').val(query);But this had a small delay and the slight inconvenience of having to replace + with a space, and to decode the URL. So I ended up with some really simple ruby, inside layout.erb:
<% query_params = request.params['query'] %>
<input type="text" [...] value='<%= query_params %>'/>