worse

That was quick: A mere day after the iPhone 7 announcement in which the much-rumored removal of the 1/8″ analog headphone jack turned out to be true, the wags at College Humor produced a pitch-perfect iPhone parody ad featuring a pretty good impression of a Jony Ive voice-over:

I laughed out loud at the part where they showed Jony Ive’s iCalendar, which has a couple of amusing scheduled items:

jony-ive-calendar

Click to see at full “al-yoo-minny-yum” size.

Hey, I still love my iPhone (and don’t forget, I run an iOS developer meetup), but if you must vent at me for blaspheming by posting this article, there is a comments selection below…

{ 0 comments }

Julia “@b0rk” Evans is programming’s zine queen!

by Joey deVilla on September 6, 2016

julia evans - how to be a wizard programmer

Solid advice from Julia Evans’ Twitter feed. Click to see the source.

If you’re a programmer (or hope to be one) and you’re not following Julia Evans (@b0rk) on Twitter, go and fix that mistake right now.

Her Twitter feed has the vibe of a zine — pronounced zeen — which is short for fanzine, which in turn is short for fan magazine. Before the web, fans of certain things — alt-rock or punk bands, cyberpunk, various cultural movements or social causes, jokes, whatever — would publish stapled-together photocopied publications as little do-it-yourself magazines:

zine covers

Zines. Click on the photo to see the source.

Here’s a definition of zine in zine format:

whats a zine

Click the photo to see the source.

Zines came about when it was easier to get your hands on a photocopier and Letraset than a computer and printer that could print more than just text. By necessity, they featured a lot of hand-drawn, hand-lettered art and comics…

zines

…and because they’re labors of love, they often feature material that you wouldn’t find in larger, more mainstream publications. Zines cover all sorts of topics, from underground culture and music to science and social responsibility…

Free Water v Bottle Water

Click to see at full size.

…and there’s at least one heavily-trafficked site that started off as a zine:

boing boing zines

Julia’s actually up and made zines about programming, what with her zine on debugging your programs using strace and her upcoming one, Linux debugging tools you’ll ❤️  (pictured below):

linux debugging tools youll love

Here’s another gem from her Twitter feed:

julia evans - how i got better at debugging

Click to see the source.

I think she’s bringing back something that we lost when why the lucky stiff decided to pull a J.D. Salinger and disappear from the web:

why upside-down ruby

But hey, this post is about Julia, so let’s end it with one more goodie from her Twitter feed!

julia evans - spy on your cpu

Click to see the source.

Once again, if you haven’t added @b0rk to your Twitter feed, you’re missing out on some great stuff!

{ 0 comments }

text field max lengths

Update, September 8, 2016: My original implementation of this solution had a memory leak problem. This new implementation doesn’t!

In this series of articles, we’ll look at making iOS text fields even better using Swift 3. We’ll start by adding an important capability that iOS text fields have been missing for far too long: the ability to limit themselves to a maximum number of characters.

.NET has a maxLength property for text fields; why can’t Cocoa?

In Microsoft’s .NET framework, TextBoxes — the .NET equivalent of Cocoa’sUITextFields —  have a MaxLength property that you can set, either visually or in code, that specifies the maximum number of characters that can be entered into them:

easy-in-visual-studio

In the setup shown above, the TextBox‘s MaxLength property is set to 3, which ensures that the maximum number of characters that can be entered into it is 3, whether the characters are inserted by the user typing or pasting them in, or by code.

Cocoa’s UIKit doesn’t provide an analog to MaxLength for its UITextFields, but there’s no reason we can’t create it ourselves. By the end of this article, we’ll haveUITextFields that feature a maxLength property that can be set either in code or Interface Builder, as shown below:

wouldnt-it-be-nice

Creating a new class that subclasses UITextField and features a maxLength property that can be edited in Interface Builder

Start a new project by doing the standard File → New → Project… dance to create a new Single View Application.

Open Main.storyboard, place a single text field on the view, select the text field and switch to the Attributes Inspector view (the inspector panel with the attributes inspector icon icon):

plain-text-field-and-attributes-inspector

The Attributes Inspector lets you edit all sorts of text field properties, but not the maximum length…yet.

Use File → New → File… to create a new Swift File

new-swift-file

…and give it the name MaxLengthTextField.swift. We’ll use it for the code of a new class, MaxLengthTextField, which will contain the necessary properties and behaviors for a text field where you can specify a maximum length.

Change the contents ofMaxLengthTextField.swift to the code below:

Here’s what’s happening in the code above — these notes go with the numbered comments:

  1. In order to create a text field with the ability to set a maximum length, we’re defining a new class — MaxLengthTextField — as a subclass of UITextField, which gives it all the properties and behaviors of a UITextField. We also specify that MaxLengthTextField adopts the UITextFieldDelegate protocol, which allows us to manage changes to the content of our new text fields. We’ll need this in order to set a limit on how much text will be allowed inside the text field.
  2. characterLimit will hold the maximum number of characters that can be entered into the text field. It’s defined as an Int? since its value may or may not be defined, and defined as private since its value will be get and set using the maxLength property.
  3. In the initializer, we specify that MaxLengthTextField will define its own UITextFieldDelegate protocol methods. We’ll make use of one of these protocol methods later on to ensure that the text field doesn’t accept any more than the specified maximum number of characters.
  4. The @IBInspectable attribute makes the maxLength property editable from within Interface Builder’s Attribute Inspector.

Trying out our new MaxLengthTextField class

With the starter code for MaxLengthTextField done, let’s try it out. Switch to Main.storyboard and put a text field on the view. With the text field still selected, select the Identity Inspector (the one with the identity-inspector icon). Within the Identity Inspector’s Custom Class section, use the Class drop-down to change the text field’s class from UITextField to MaxLengthTextField:

custom-class

With this change, the text field is no longer an instance of UITextField; it’s now an instance of MaxLengthTextField. This becomes obvious when you switch to the Attributes Inspector (the inspector with the attributes inspector icon icon). Near the top of the Attributes Inspector, you’ll see that the text field has a new section of properties, Max Length Text Field, and within it, a new property called Max Length:

attributes-inspector

Note that Interface Builder did a couple of things in response to our marking the maxLength property with the @IBInspectable attribute:

  • It created a new section of the Attributes Inspector titled Max Length Text Field, deriving its name from the MaxLengthTextField class, where you can edit its @IBInspectable properties.
  • It created a new property field titled Max Length, which lets you edit MaxLengthTextField‘s maxLength property.

You’ll want to keep the way Xcode names Attribute Inspector fields in mind when naming classes and properties that you want to be editable within Interface Builder.

You can set the Max Length property to any valid integer value. It won’t have any apparent effect if you run the app; right now, all it does is get and set the value of MaxLengthTextField‘s private characterLimit variable. Let’s make Max Length actually do something.

Making the maxLength property limit the number of characters allowed in a text field

Update the code in MaxLengthTextField.swift so that it contains the following:

Here’s what’s happening in the code above — these notes go with the numbered comments:

  1. The actual functionality of MaxLengthTextField is contained within textField(_:shouldChangeCharactersIn:replacementString:), one of the methods made available by adopting the UITextFieldDelegate protocol. This method is called whenever user actions change the text field’s content, and its Bool return value specifies if the text change should actually change place. We’ll use this method to limit the number of characters that can be entered into the text field — if user changes would cause the number of characters in the text field to exceed characterLimit, it returns false; otherwise, it returns true.
  2. Get to know and love the guard statement and the “early return” style of programming; you’re going to see a lot of it in a lot of Swift coding. Here, we’re using guard to filter out cases where the user’s changes are of zero length, which means that characters aren’t being added, but deleted. In this case, we don’t have to see if the user’s changes will cause the text field to exceed its set maximum number of characters. We do want the method to return true in order to allow the user’s deletions to take place.
  3. If we’ve reached this point of the method, it means that the user has made additions to the text field. We should see if these changes would cause the text field to contain more than characterLimit characters. The first step in this process is getting the text currently in the text field. We use the nil coalescing operator?? — to assign the contents of the text field if they are not nil, or the empty string if they are nil.
  4. Now that we have the current text, we’ll determine the prospective text — the text that would result if the changes were accepted.
  5. Finally, we use the the length of the prospective text to decide whether to allow the changes or not.

If you include MaxLengthTextField.swift in any of your iOS projects, it’s pretty simple to turn ordinary text fields into ones with maxLength properties that you can set either GUI builder-style in Interface Builder or in code, using myTextField.maxLength = n syntax, just like the .NET people do.

Happy text field coding!

A sample project showing Visual Studio-style text field maximum lengths in action

text field max lengths

If you’d like to try out the code from this article, I’ve created a project named Swift 3 Text Field Magic 1 (pictured above), which shows our new MaxLengthTextField subclass in action. It’s a quick-and-dirty single-view app that presents 4 text fields:

  1. A text field without a set maximum length.
  2. A text field with a 1-character maximum length, with the Max Length property set in Interface Builder.
  3. A text field with a 5-character maximum length, with the maxLength property set in code (in the view controller’s viewDidLoad method).
  4. A text field with a 10-character maximum length, with the Max Length property set in Interface Builder.

Give it a try, learn what makes it tick, and use it as a jumping-off point for your own projects!

xcode download

You can download the project files for this article (61KB zipped) here.

Coming up next in this series…

In the next installment in this series, we’ll build on what we’ve made so far to create a text field that accepts only a defined set of characters.

{ 4 comments }

Samsung Galaxy Note 7 recalled worldwide over battery combustion issue

galaxy note 7 recall

In response to reports of the Galaxy Note 7 “phablet” catching fire while charging, Samsung has announced that they will recall and “voluntarily replace” users’ current devices with new ones “over the coming weeks”. Samsung reports that they are aware of 35 faulty Galaxy Note 7 devices and says that they’ve found 24 problem devices for every million sold. At the time of writing, 2.5 million Note 7s have been sold.

Here’s an ABC News report on the issue:

Samsung says that it will take two weeks to set up the recall. After that, replacement devices will be made available, with replacement dates varying by country. If you are concerned about your Galaxy Note 7, Samsung advises that you contact your nearest Samsung service center. If you purchased your Note 7 from a carrier, you should keep an eye out for announcements (T-Mobile has already issued one).

The next great platform is the one that we already have

the next great platform

Josh Elman, a partner at Silicon Valley venture firm Greylock Partners, makes a convincing argument that the next great platform just happens to be the current great platform: mobile.

History is his guide. Just as some people say “mobile is over” now, people were saying “the web is over” a little over a decade ago — and then Web 2.0, the LAMP stack, AJAX, and social networking came along. A little over ten years before that, some people were worried that desktop computing had plateaued — and then CD-ROM multimedia and the web came along.

There are still plenty of needs that aren’t being met by mobile today, but they could be met by mobile tomorrow.

Speech is 3x faster than typing for English and Mandarin text entry on mobile devices

hello computer

A recent experiment at Stanford University revealed some interesting results: entering text via speech recognition into mobile devices yielded faster input rates and lower error rates. If these results are repeated in other experiments, we may find a radical change in the way we interact with our phones in the near future.

They produced a two-minute video summarizing their findings:

this article also appears in the GSG blog

{ 0 comments }

A list of iOS development podcasts, September 2016 edition

by Joey deVilla on September 1, 2016

podcast

For those of you looking for podcasts on iOS development, here’s a list of podcasts that were active at the time of writing. If I’ve missed any, please drop me a line or let me know in the comments, and I’ll update the list!

Podcast Description Number of episodes at the time of writing
The App Guy Podcast Paul Kemp’s long-running interview podcast featuring app developers and app development authors. 485
AppMasters App entrepreneur Steve Young’s podcast, which he started so he could interview app creators he admired. Each podcast is an interview with an app developer. 471
Build Phase A weekly podcast hosted by iOS developers Mark Adams, Jack Nutting, and Gordon Fontenot. 102
Concepts in Code Hank Turowski and BJ Miller discuss best practices for iOS programming and programming in general. 16
Core Intuition A long-running indie software developer podcast covering iOS, MacOS and other Apple technologies hosted by Daniel Jalkut and Manton Reece, who’ve developed a comfortable, conversation podcasting style and rhythm over their long history together. 247
iOS Bytes A podcast with bite-sized episodes (each one about 5 minutes long) covering the latest iOS news. Of all the podcasts in this list, this one may be on hiatus or cancelled — the most recent episode was posted on June 16, 2016. It’s hosted by Zach Cowart and John Friskics. 119
iOhYes Darryl Thomas’ and John Sextro’s podcast, featuring “news, tips, and rants for professional iOS/Mac developers, with something for enterprise and indie developers alike”. 117
iPhreaks A group discussion podcast featuring a varying panel that discusses iOS development, related technologies, Apple, tools, practices, and code. 167
More Than Just Code A mix of current issues, news, notes, and tech tips discussed by Aaron VeghJaime LopezMark Rubin, and Tim Mitra (and sometimes Greg Heo and Tammy Coron). Their podcasts are pretty freewheeling and go through many topics; episodes are typically at least an hour long. 106
RayWenderlich.com Podcast The podcast of the leading iOS (and expanding to other platforms) tutorial site, hosted by Mic Pringle and Jake Gundersen. In the current format, the podcast covers two topics per episode, with 20 minutes devoted to each topic. 59
Release Notes Joe Cieplinski and Charles Perry‘s podcast about the business of independent iOS and MacOS development. They cover “nspiration, design, trends, and tools — everything but the code”, and “topics for the new or curious independent developer looking to make his or her way in the iOS and Mac ecosystem”. 172
Under the Radar Full-time independent iOS developers David Smith (Activity++, Pedometer++, Sleep++ and more) and Marco Arment (Overcast for iOS, Quitter for MacOS) pick a topic about some aspect of indie app development and have a back-and-forth conversation about it. Each episode never goes longer than 30 minutes. 42

{ 1 comment }

Yup.

by Joey deVilla on August 31, 2016

most used language in programming

{ 0 comments }

In this article, we’ll expand on material covered in the three previous articles in this series:

A more readable way to work with Dates and DateComponents

Suppose we want to find out what the date and time will be 2 months, 3 days, 4 hours, 5 minutes, and 6 seconds from now will be. If you recall what we covered in the last installment in this series, you’d probably use code like this:

In the code above, we did the following:

  • We created an instance of a DateComponents struct.
  • We set its properties so that it would represent a time interval of 2 months, 3 days, 4 hours, 5 minutes, and 6 seconds.
  • We then used Calendar‘s date(byAdding:to:) method to add the time interval to a Date.

This code wouldn’t look out of place in a lot of other programming languages, but we can do better in Swift. What if I told you that by defining a few helper functions, you can turn the code above into the code below?

Or this code?

I’d much rather write the code above. This article will cover the code necessary to make this kind of syntactic magic possible.

Overloading + and - so that we can add and subtract DateComponents

First, let’s write some code that allows us to add and subtract DateComponents. Start a new playground and enter the following code into it:

In the code above, we’ve overloaded the + and – operators so that we can add and subtract DateComponents. I derived these functions from Axel Schlueter’s SwiftDateTimeExtensions library. He wrote them when Swift was still in beta; I updated them so that they compile with the current version and added a couple of tweaks of my own.

The addition and subtraction operations are so similar and so tedious, which is a sign that there’s an opportunity to DRY up the code. I factored out the duplicate code from both the + and - overloads and put it into its own method, combineComponents, which does the actual DateComponents addition and subtraction.

You may have noticed a lot of ?? operators in the code for combineComponents. ?? is referred to as the nil coalescing operator, and it’s a clever bit of syntactic shorthand. For the expression below:

let finalValue = someOptionalValue ?? fallbackValue

  • If someOptionalValue is not nil, finalValue is set to someOptionalValue‘s value.
  • If someOptionalValue is nil, finalValue is set to fallbackValue‘s value.

Let’s confirm that our code works. Try out the following code:

Overloading - so that we can negate DateComponents

Now that we can add and subtract DateComponents, let’s overload the unary minus so that we can negate DateComponents:

With this overload defined, we can now use the unary minus to negate DateComponents:

Overloading + and - so that we can add Dates and DateComponents and subtract DateComponents from Dates

With the unary minus defined, we can now define the following operations:

  • Date + DateComponents
  • DateComponents + Date
  • DateDateComponents

Note that we didn’t define an overload for subtracting Date from DateComponents — such an operation doesn’t make any sense.

With these overloads defined, a lot of Date/DateComponents arithmetic in Swift becomes much easier to enter and read:

Extending Date so that creating dates is simpler

Creating Dates in Swift is a roundabout process. Usually, you end up creating them in one of two ways:

  • Instantiating a DateComponents struct and then using it to create a Date using Calendar‘s date(from:) method, or
  • Creating a String representation of the Date and then using it to create a Date using DateFormatter‘s date(from:) method.

Let’s simplify things by extending the Date struct with a couple of convenient init method overloads:

With these methods, initializing Dates is a lot more simple:

Overloading - so that we can use it to find the difference between two Dates

When we’re trying to determine the time between two given Dates, what we’re doing is finding the difference between them. Wouldn’t it be nice if we could use the - operator to find the difference between Dates, just as we can use it to find the difference between numbers?

Let’s code an overload to do just that:

Let’s test it in action:

Extending Int to add some syntactic magic to date components

We’ve already got some syntactic niceties, but the real Swift magic happens when we add this code to the mix:

This additions to Int allow us to convert Ints to DateComponents in an easy-to-read way, and with our overloads to add and subtract DateComponents to and from each other, and to add Dates to DateComponents, we can now perform all sorts of syntactic magic like this:

Extending DateComponents to add even more syntactic magic: fromNow and ago

And finally, a couple of additions to the DateComponents struct to make Date/DateComponent calculations even more concise and readable:

Here are these additions in action:

Wrapping it all up

Here’s the playground containing all the code we just worked with:

 

{ 0 comments }