[@"πŸ˜›" length]

NSString and Unicode from objc.io issue 9:

At its most basic level, the Unicode standard defines a unique number for every character or symbol that is used in writing, for nearly all1 of the world’s writing systems. The numbers are called code points and are written in the form U+xxxx where the xxxx are four to six hexadecimal digits. For example, the code point U+0041 (65decimal) stands for the letter A in the Latin alphabet (same as ASCII) and U+1F61B represents the emoji named FACE WITH STUCK-OUT TONGUE, or πŸ˜›.
[...]
Unicode was originally conceived as a 16-bit encoding, providing room for 65,536 characters.
[...]
The Unicode code space was later extended to 21 bits (U+0000 to U+10FFFF) to allow for the encoding of historic scripts and rarely-used Kanji or Chinese characters.
[...]
The code space is divided into 17 planes with 65,536 characters each. Plane 0 is called the Basic Multilingual Plane (BMP) and it is where almost all characters you will encounter in the wild reside, with the notable exception of emoji.
[...]
UTF-16 itself is a variable-width encoding. Each code point in the BMP is directly mapped to one code unit. Since the BMP encompasses almost all common characters, UTF-16 typically requires only half the memory of UTF-32. The rarely used code points in other planes are encoded with two 16-bit code units. The two code units that together represent one code point are called a surrogate pair.
[...]
Since virtually all characters in modern use reside in the BMP, surrogate pairs were very rare encounters in the real world. However, this has changed a few years ago, with the inclusion of emoji into Unicode, which are in Plane 1. Emoji have become so common that your code must be able to handle them correctly.

NSString *s = @"\U0001F30D"; // earth globe emoji 🌍
NSLog(@"The length of %@ is %lu", s, [s length]);
// => The length of 🌍 is 2

Sigh. Small consolation that whenever something is a mess, it's always a bigger mess in JavaScript.

trash

This is not true...

From: smith@canon.co.uk (Mark Smith)  
Newsgroups: talk.bizarre  
Subject: Re: Real Unix Users

I once mistyped 'cal 1976' as 'su -c "mount -a; rm -rf /"' and then I fainted, accidentally spelling out the root password with my forehead just before my right hand fell on the return key and the cigarette dropped out of my left hand onto the backup tapes.

No wonder they call it an unfriendly operating system.  

This is true...

Once upon a time, I was unwisely logged in as root on a production system. For reasons that made sense at the time, I typed the command

rm -r ../bin

... and saw the message

rm: cannot remove directory: '..'

... because I fat-fingered a space in the middle and actually typed

rm -r .. /bin

... and in the time it took to realize that, most of /bin was gone. It turns out that Linux doesn't run very well without /bin.

Now I spend most of my time on OS X without the root user enabled, but rm is still there, eager to do what I tell it to do, not what I want it to do.

And that's why this tiny script is my best friend.

#! /bin/sh

DIR=~/.Trash/`date "+%y%m%d.%H%M%S"`

mkdir $DIR

while [ $# -ne 0 ]  
do  
  mv "$1" $DIR
  shift
done  

Save it as trash on your path, and use it instead of rm:

$ trash aFile aDirectory anotherFile etc

It will create a timestamped directory in your Trash and move all of its arguments there, out of the way but not actually deleted until the Trash is emptied.

Update: Just install the node package trash.

yard sale finds

The whole thing. All of it.

front back

network activity indicator

Given that iOS manages everything else in the status bar, it's a mystery to me why each application is supposed to implement logic for displaying the network activity indicator.

Fortunately, AFNetworking will do it for you:

AFNetworkActivityIndicatorManager manages the state of the network activity indicator in the status bar. When enabled, it will listen for notifications indicating that a network request operation has started or finished, and start or stop animating the indicator accordingly. The number of active requests is incremented and decremented much like a stack or a semaphore, and the activity indicator will animate so long as that number is greater than zero.

recursive blocks in Objective-C

Define another reference to the block, to be captured in the block itself.

The __block attribute ensures that weakBlock isn't captured by value before being set.

The __weak attribute prevents the two block references keeping each other alive forever.

__weak __block void (^weakBlock)(int i);

void (^strongBlock)(int) = ^(int i) {
  NSLog(@"%d", i);
  if (i < 3) {
    weakBlock(i+1);
  }
};

weakBlock = strongBlock;
strongBlock(1);

Output:

1
2
3

From a discussion here.