Archive for June 1, 2015

Monday, June 1, 2015

@weakify and @strongify Macros

Arkadiusz Holko:

Original implementation of @weakify and @strongify macros is complex because they accept more than one parameter. To make the analysis simpler, we’ll introduce our own versions, accepting only one parameter each:

#define weakify(var) __weak typeof(var) AHKWeak_##var = var;

#define strongify(var) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wshadow\"") \
__strong typeof(var) var = AHKWeak_##var; \
_Pragma("clang diagnostic pop")

[…]

In the block, self is overshadowed by a local variable with the same name. Then, self can be used safely inside the block, because it references that local variable, which is held strongly, but lives only until the block ends executing. Even the code not written by us can use self safely, e.g. NSAssert macro.

How Not to Crash #7: Dealing With Nothing

Brent Simmons:

With some OS X release (10.5, I think) Apple changed the format for crash logs on disk. I think they had been one file per app, and Apple switched to one file per crash. I had to write new code to handle the new format.

I made the change. It went to beta testers, who used the app extensively. Weeks passed. All good.

Then, on the day I released this version, I got a ton of reports from people who said, “It’s crashing on launch! But it works fine after launching it again.”

Update (2015-06-02): Mark Berstein:

If you do find yourself writing lots of if(thing)... tests, though, you might be better off passing a Null Object -- a subclass of the expected class that does nothing, and does it quickly.

[…]

So, instead of checking, Tinderbox initializes views with an instance of NoLayoutPolicy. If a view is being constructed and isn't quite ready to do real work, we can still call Prepare() in perfect safety. If we’re about to disassemble a window, we can replace its layout policy with a fresh NoLayoutPolicy, confident that if someone tries to refresh the window we’re demolishing, there will be a NoLayoutPolicy standing read to do nothing.

Apple Watch Heart Rate Sensor Errors

Kirk McElhearn:

Now, Apple has updated a technical document about how the heart rate sensor works. They are no longer saying that the Apple Watch reads your heart rate every ten minutes; instead, they say this:

Apple Watch attempts to measure your heart rate every 10 minutes, but won’t record it when you’re in motion or your arm is moving.

This is quite surprising. Why wouldn’t it take a reading when you’re in motion? After all, the goal of a fitness tracker is to motivate you to be active. This means that the Apple Watch cannot make any claim to realistically calculate your activity based on your heart rate.

Update (2015-06-02): Kirk McElhearn:

To many users, it looks like Apple pulled a bait-and-switch, promising a certain feature and not delivering it. Apple needs to say whether the change is because of faulty heart rate sensors – which means they have a bigger issue – or because of battery life. And if it’s the latter, they should allow users to choose whether or not the Apple Watch checks their heart rate every ten minutes. Let users decide how they want their battery usage to work.

However, if the heart rate sensors are faulty, simply turning them off, after promising this feature, is a mistake. They should fix them, whether through a software update, or by exchanging the devices. They promised a feature, and they can’t simply pretend that they never did so.

OSStatus.com

OSStatus.com (via Seth Willits) is a great resource for quickly looking up error codes. It supports not only the traditional OSStatus error codes, but also NSError codes from NSCocoaErrorDomain, kCFErrorDomainCFNetwork, NSPOSIXErrorDomain (errno.h), etc.

Update (2015-10-04): Marco Masser:

This [LaunchBar] action uses osstatus.com to look up information about error codes on Apple’s platforms. This is useful if you’re a developer who wants to look up some error code or constant but you don’t know in which framework or in which header it is defined.

As a bonus, if you have Xcode installed (either as /Applications/Xcode.app or /Applications/Xcode-beta.app), the search results let you browse the related frameworks and header files directly in the respective SDK.