Archive for July 6, 2014

Sunday, July 6, 2014 [Tweets] [Favorites]

iOS 8 Keyboards Can’t Implement SwipeSelection

Andrei Puni (via Clark Goble):

Your custom keyboard does not have access to the view hierarchy of the input, it cannot control the cursor and select text.

I’m really surprised that Apple hasn’t improved text selection yet.

Update (2014-07-08): Ole Zorn:

Looks like it's possible to move the cursor with a custom keyboard in beta 3. :)

Strings in Swift

Ole Begemann:

In this article, I want to take a closer look at how strings are handled in Swift. I see this as a follow-up to a piece titled NSString and Unicode that I wrote for a while ago. Please refer to that article for a more thorough explanation of the Unicode features I mention below. I also assume that you have read the chapter on Strings and Characters in Appleʼs Swift book.


Swift strings do not have a length property. You can use the global countElements() function (which works on any Sequence, including strings) to count the number of Characters in a string. In the following example, countElements() counts the number of characters correctly whereas NSString does not. Because of the differences in what constitutes a character between String and NSString, NSStringʼs length property has been renamed utf16count when you access it on a Swift string.


Because of the way Swift strings are stored, the String type does not support random access to its Characters via an integer index — there is no direct equivalent to NSStringʼs characterAtIndex: method. Conceptually, a String can be seen as a doubly linked list of characters rather than an array.


Character and range indices are based on the opaque String.Index type, which implements the BidirectionalIndex protocol (an extension of the ForwardIndex protocol). To construct an index for a specific position, you have to first ask the string for its startIndex and then use the global advance() function to iterate over all characters between the beginning of the string and the target position (again, an O(N) operation; advance() will simply call succ() several times):


Another implication of this design is that String.Index values are not freely interchangeable between strings.

Update (2014-07-22): Ole Begemann:

I have completely rewritten my article on Strings in Swift from earlier this month. Xcode 6 beta 4 fundamentally changed how the String and Character type handle Unicode characters that are composed of multiple code points.

Secrets of Swift’s Speed

Mike Ash:

A simple array index is quite a bit faster than objc_msgSend, even with its fancy caching and finely tuned assembly code, so this is a nice performance win.

The call to testmethod2 is even nicer. Because it’s declared @final, the compiler knows that nothing can override it. No matter what happens, the call will always go to the implementation in Class. Thus the compiler doesn’t even need to emit a vtable lookup. It can just make a normal function call that goes straight to __TFC9speedtest5Class11testmethod2fS0_FT_T_, which is the mangled name of the testmethod2 implementation in my test case.


The different method call semantics allow speed improvements beyond simply using faster dispatch techniques. Because the compiler can better understand where control goes, it can optimize the code better. It can potentially inline method calls or even eliminate them entirely.


Swift omits the _cmd parameter, freeing up an additional argument register for more useful purposes. For methods that take several explicit parameters (three or more for ARM, five or more for x86-64, and seven or more for ARM64), this means a small performance win on every call due to better use of argument registers.

Goldman Says Google Blocked E-mail With Leaked Client Data

Jonathan Stempel (via Nick Heer):

“Google complied with our request that it block access to the email,” Goldman spokeswoman Andrea Raphael said. “It has also notified us that the email account had not been accessed from the time the email was sent to the time Google blocked access. No client information has been breached.” A Google spokeswoman declined to comment.

The Rationale Behind Standard Paper Sizes Like A4 and A3

Tracey Bryan:

The A series of paper sizes are designed so that when you cut one in half, you get two pieces of the next-smallest-size, and every size has height and width in the same proportion. A little math reveals that one can achieve this by having the height and width in the ratio sqrt(2):1, or approximately 1.414:1.


In the case of the A series of paper sizes, an A0 piece of paper is exactly 1 square metre, requiring width x width x 1.414 = 1, which gives a width of 84.1cm and height of 118.9cm (to the nearest mm).


Another interesting consequence is that it becomes easy to calculate the weight of single sheets. Standard photocopy paper is usually 80gsm (grams per square metre), thus an A0 sheet, being 1 square metre, weighs 80g. An A1 weighs 40g, A2 is 20g, A3 is 10g, and A4 is 5g. (And so on.)

Brian Beej Hall:

This “halving” situation is also fantastic for envelope sizes. A C4 envelope will hold a sheet of A4 paper. A C5 will hold A5… or A4 folded in half. A C6 will hold A6, or an A4 folded in quarters.

John Whitehead:

Because the ratio of the sides of all A size papers is root 2, or 1.41, it’s always the case that expanding a document from one size to the next largest needs a factor of 141% dialled into your photocopier or typed into Illustrator or whatever. Reducing from one A size to the next smallest requires a factor of 1 over root 2, or 0.71, or 71%.

Rob Choucroun:

Say, for example, you have a map printed on A1 paper and the map is at a scale of 1:1,000. If you want to print the map on smaller paper, because of the ratio of the paper sizes, you can drop the paper two sizes (to A3) and the scale will reduce to 1:2,000.

See also Wikipedia’s articles about paper size and ISO 216.

Inspecting Yosemite’s Icons

Nick Keppol:

The shapes and sizes of the icons have been adjusted to provide a better visual rhythm on screen. I have not seen it published anywhere, but there appears to be a grid system that helps achieve this consistency.


There are three basic shapes utilized for application icons across the system. A circle, a square, and a tilted rectangle. Of course, Apple is consistently inconsistent and deviates from this system in a few places, but they are clearly outliers.


The old tilted rectangle icons, like the one on the left, are fully placed in 3D space, with natural perspective. They have a vanishing point on all axis. Put another way, if you drew a rectangle, and rotated it -11°, you would still have to skew it in perspective to match the shape.

The Yosemite icons appear orthographic at first, but with depth. They are a straight-on view of an object, with no perspective on the axis facing “the camera”. If you take a rectangle and rotate it -9°, you don’t have to skew anything, other than false 3D depth. (e.g., add the pages of the book, a drop shadow, an edge, etc.) They are much simpler in look, and easier for artists to draw.


Grey scale is out — warm and cool tones are in. It’s been a popular look in Hollywood blockbusters: yellow/orange highlights, blue/teal shadows. The new Yosemite icons use similar tonal shifts with their metal materials. If we consider these icons as materials, this tone represents an environment reflection — not merely a color effect.

A First-Person Engine in 265 Lines

Hunter Loftis (via Hacker News):

In this article, we’ll compose a first-person exploration from scratch, quickly and without difficult math, using a technique called raycasting. You may have seen it before in games like Daggerfall and Duke Nukem 3D, or more recently in Notch Persson’s ludum dare entries.

Raycasting feels like cheating, and as a lazy programmer, I love it. You get the immersion of a 3D environment without many of the complexities of “real 3D” to slow you down. For example, raycasts run in constant time, so you can load up a massive world and it will just work, without optimization, as quickly as a tiny world. Levels are defined as simple grids rather than as trees of polygon meshes, so you can dive right in without a 3D modeling background or mathematics PhD.

A Closer Look at Android RunTime (ART)

Andrei Frumusanu:

The Android RunTime, ART, is the successor and replacement for Dalvik, the virtual machine on which Android Java code is executed on. We’ve had traces and previews of it available with KitKat devices since last fall, but there wasn’t much information in terms of technical details and the direction Google was heading with it.


First, ART is designed to be fully compatible with Dalvik’s existing byte-code format, “dex” (Dalvik executable). As such, from a developer’s perspective, there are no changes at all in terms of having to write applications for one or the other runtime and no need to worry about compatibilities.

The big paradigm-shift that ART brings, is that instead of being a Just-in-Time (JIT) compiler, it now compiles application code Ahead-of-Time (AOT). The runtime goes from having to compile from bytecode to native code each time you run an application, to having it to do it only once, and any subsequent execution from that point forward is done from the existing compiled native code.

Of course, these native translations of the applications take up space, and this new methodology is something that has been made possible today only due to the vast increases in available storage space on today’s devices, a big shift from the early beginnings of Android devices.

This shift opens up a large amount of optimizations which were not possible in the past; because code is optimized and compiled only once, it is worth to optimize it really well that one time. Google claims that it now is able to achieve higher level optimizations over the whole of an applications code-base, as the compiler has an overview of the totality of the code, as opposed to the current JIT compiler which only does optimizations in local/method chunks.


An important difference that Google is applying over Apple, at least inside VM runtime applications, is that they are using reference compression to avoid the usual memory bloat that comes with the switch to 64-bit. The VM retains simple 32-bit references.

iOS 8 Share Extensions

Rene Ritchie:

With iOS 8 and Extensibility, gone are the days when Apple had to make a deal with social networks and laboriously integrate them one and a time into iOS. Now, any app you download from the App Store can hook into the Share Sheets and give you the option to share or upload your content with other members and to the service.


Because of custom sharing extensions, you also get the ability to customize your sharing options. Scroll all the way to the right on a Share Sheet and you’ll see a special “More” icon. Tap it and you’re taken to the Activities panel where you can toggle on or off all the sharing options (with the exception of Messages and Mail), and move all of them around into any order you like.


System defaults are both quick to implement and offer a lot of functionality, like image preview, text entry, audience picker, etc. “for free”. They also help maintain continuity of experience. Custom sheets are more work but can leverage code from the existing app and better show off a service’s branding. That can be useful in continuously, visually reminding someone which service they’re sharing to throughout the process.


Both web URL and web pages are supported. The first is all about sharing a link. The second is all about pulling the data from the web page itself. Developers can, via JavaScript, determine which parts of a webpage their extension wants.

Swift Instance Methods Can Shadow Functions

Dan Wineman (via Twitter):

Swift allows you to call instance methods from other instance methods with no scope prefix, indistinguishably from function calls. This leads to the dangerous situation of function calls being shadowed when identically-named instance methods are added to a base class.

This seems like a really bad idea. In other areas, Swift adds more protections for this sort of thing.

Swizzling and Touch Forwarding

Peter Steinberger:

This works as expected in most cases, but has the issue that the original implementation will be called with a different _cmd than it expects. This can be a problem when _cmd is actually used, such as in the touch forwarding logic. I learned this the hard way after swizzling touchesMoved:withEvent: to inject additional logic. The app crashed with the popular doesNotRecognizeSelector: exception.

Instead of exchanging the method implementations, one could replace the old method and store its IMP outside the runtime. But then:

We are now modifying the touchesBegan:withEvent: method of our custom UIView subclass. There is no default implementation yet, so we get the IMP from UIResponder and then manually call this. Imagine if at some later point, somebody else would swizzle touchesBegan:withEvent: on UIView directly using the same technique. Assuming UIView has no custom touch handling code, they would get the IMP from UIResponder and add a new method to UIView. But then our method gets called, which already captured the IMP of UIResponder and completely ignores the fact that we modified UIView as well.

Perhaps I’m missing something, but it seems like it would be possible to exchange the method implementations. When you need to call the old method, use the new selector to fetch the old IMP. Then call the old IMP passing in the old selector. Then the old method would still receive its expected selector, and you would still be safe if other code swizzles the same method.

iOS 8 Privacy Updates

Luis Abreu:

The latest updates to iOS 8 and OS X Yosemite introduce some very welcomed changes to the way Security and Privacy is dealt with on these platforms and may serve as an inspiration for others.

I’ve gathered this information by watching over 17 hours of WWDC 2014 sessions and carefully reviewing, analyzing what was said, and writing a huge number of notes on Security, Privacy, UX and other areas which I will be publishing here in the coming weeks.


As a side note: you can now direct the user directly to the applications’s privacy controls in iOS Settings from your app without having to display step-by-step instructions for the user to follow.

Sounds like this sort of guidance won’t be as necessary.

Facebook Big Likes

Paul Kafasis:

Alas, Facebook Messenger 6.1 has already been released, and it obliterates all reference to version 6’s revolutionary upgrades. Let this post serve as a testament to the ingenuity on display at Facebook, in the form of “Big Likes”.

iCloud Drops Support for Third-Party AIM Clients

Dan Moren:

Thanks to enhanced security that Apple has put in place as of July 1, folks with those specific [] usernames can no longer connect to the AIM service with clients other than Messages running on OS X 10.7.2 or later.


If you don’t want to upgrade your version of OS X or switch to Messages, your alternative is to create a new AIM account. Of course, that requires that you re-construct your buddy list, which may be a not insignificant amount of work.