Archive for November 7, 2016

Monday, November 7, 2016

Objective-C id as Swift Any


In Swift 3, the id type in Objective-C now maps to the Any type in Swift, which describes a value of any type, whether a class, enum, struct, or any other Swift type. This change makes Objective-C APIs more flexible in Swift, because Swift-defined value types can be passed to Objective-C APIs and extracted as Swift types, eliminating the need for manual “box” types. These benefits also extend to collections: Objective-C collection types NSArray, NSDictionary, and NSSet, which previously only accepted elements of AnyObject, now can hold elements of Any type. For hashed containers, such as Dictionary and Set, there’s a new type AnyHashable that can hold a value of any type conforming to the Swift Hashable protocol.


Property lists, JSON, and user info dictionaries are common in Cocoa, and Cocoa natively represents these as untyped collections. In Swift 2, it was necessary to build Array, Dictionary, or Set with AnyObject or NSObject elements for this purpose, relying on implicit bridging conversions to handle value types[…] Swift now imports Cocoa APIs as accepting collections of Any and/or AnyHashable, so we can change the collection type to use [AnyHashable: Any] instead of [NSObject: AnyObject] or NSDictionary, without changing any other code.


Any does not have the same magic method lookup behavior as AnyObject. This may break some Swift 2 code that looked up a property or sent a message to an untyped Objective-C object.

How Not to Crash

Pádraig Kennedy:

It’s good practice for programmers to figure out how to recreate crashes that they’re trying to fix. This can involve temporarily rewriting parts of the code to behave in an artificial way that makes the crash more likely. If we can reliably see the crash happen, it goes some distance to confirming suspicions and it gives us something to test potential fixes against. The alternative is to try a fix blindly, release it, and wait to see if we get crash reports.


In this case, I am aware of no way to trigger the suspension of an app that is connected to the debugger. In fact the debugger prevents suspension, and the simulators don’t accurately simulate it. Without the debugger, the only option is to experiment and then review logs on the device.


Apps that access files in a shared container while the app is running in the background should create a background task and not assume that the 30 second completion block time covers them. To work around this, developers can create a background task using the beginBackgroundTaskWithName:expirationHandler method on UIApplication and call endBackgroundTask when the background work is finished.


Additionally, Kevin also suggested that apps should close the database when they go into the background as a way to ensure they’ve finished flushing data and to surface rare bugs more reliably[…]

Fake Retail Apps Are Surging Before Holidays

Vindu Goel (via Hacker News):

The counterfeiters have masqueraded as retail chains like Dollar Tree and Foot Locker, big department stores like Dillard’s and Nordstrom, online product bazaars like and Polyvore, and luxury-goods makers like Jimmy Choo, Christian Dior and Salvatore Ferragamo.


Some of them appeared to be relatively harmless — essentially junk apps that served up annoying pop-up ads, he said.

But there are serious risks to using a fake app. Entering credit card information opens a customer to potential financial fraud. Some fake apps contain malware that can steal personal information or even lock the phone until the user pays a ransom. And some fakes encourage users to log in using their Facebook credentials, potentially exposing sensitive personal information.


Many of the fake retail apps have red flags signaling that they are not real, such as nonsensical menus written in butchered English, no reviews and no history of previous versions.

Benjamin Mayo:

Detecting malicious activity is a hard problem at scale — the App Review process has to handle thousands of apps every day — but it does seem like Apple could be doing more to protect the store from counterfeit software … especially with big, well-known, brands like Nike or Puma.

Update (2016-11-08): Nick Heer:

Contrary to the article, these apps did not appear “just in time for the holidays” — rather, that’s when the Times and New York Post noticed them. Even though Apple has now removed the apps from the App Store, there’s evidence around the web that these apps have been in the store since mid-September.

Better Xcode Run Script Build Phases

Giovanni Lodi:

The single most effective action you can take to improve your build scripts is to extract them into their own files.


Xcode will name every new run script phase as “Run Script”, but that can be quite confusing when you have more than one. Double click on the “Run Script” header to reveal a text field you can use to rename your phase.


The idea is to have a single script invocation in Xcode, calling a script that will then take care of executing the single scripts.

This technique will help you grow your script, change their order, etc. in a tidy and understandable way.


You can make your scripts output compilation errors or warnings the same way Xcode does. I learnt this a while ago from this post.