Regular Expressions Helper

NOTE: if you came here from an external site, you probably want the Regular Expressions Helper download/support page instead. Annoyingly, some Mac sites are direct-linking to this page, incorrectly

I tried some of the standard OS X apps for doing regexp, and they are all either too expensive, or massively over-featured. Or just far too difficult to understand.

I’ve integrated regexp behind the scenes in several iPhone apps, and I’m pretty confident using the libs. So, this seemed like a good excuse to make a first native OS X app.

Now available on the Mac App Store (http://www.apple.com/mac/app-store/?id=429461864)

Current features:

  1. The text you want to change can either be a file (drag/drop to the app), or text (copy/paste into the box)
  2. While you type your regexp, the app constantly updates the output to show you what would happen (great for checking you’ve got the regexp syntax you intended)
  3. When you’re done, select the output box and copy/paste the results to wherever you need them

Xcode4: make a library in one file that works on BOTH device AND simulator

I’ve just updated my epic stack overflow answer with the full script + instructions for Xcode 4.

Here’s a quick summary without all the question/answer stuff from the SO page.

We can – theoretically – build a static library into a SINGLE FILE that includes: simulator, iPhone, and iPad.

Advantages

  1. just ONE FILE to ship to your developers / clients for including in their apps
  2. never accidentally send “the wrong” file, and have them complain of build failures
  3. when you update, they only have to overwrite one file, in one place, not two (I’ve noticed that with two files, 3rd party devs often accidentally only overwrite one of them, and we start to see version-mismatch problems)
  4. no mucking about switching back-and-forth between builds / schemes: just hit “build”, and you’re done. Easy!

However, Apple has no documentation on this that I can find, and Xcode’s default templates are NOT configured to do this. So, with some help from Eonil on SO, I built a script that you can copy/paste into an Xcode project, and which does this for you.

NB: I’ve only just started using Xcode4, and I suspect there MAY be a way to achieve this in the Xcode4 GUI. It used to be possible in Xcode3, and then one day Apple removed the feature entirely, with no warning. So, even if it exists in Xcode 4 today, it may disappear again. This script is your backup plan ;) .

Usage – Xcode 4

1. Create a static lib project
2. In the main sidebar, select the xcodeproject line
3. Select the TARGET that appears in a new sub-sidebar
4. Xcode 4.4 onwards Click “Add Build Phase”
5. Xcode 4.4 onwards Select “Add Copy Headers”
6. Click “Add Build Phase” (it’s hiding on bottom right corner of screen)
7. Select “Add Run Script”
8. Expand the new “Run Script” box that appeared as last item in the list on screen
9. Copy/paste the script below into the box

2012: Xcode 4.4 onwards: Latest Xcode has broken the creation of new libraries. Apple no longer creates the headers phase, which is ESSENTIAL to all libraries – until they fix Xcode, you have to add this manually. Make sure you add it ABOVE the “Run Script” phase.

Usage – Xcode 3

1. Create a static lib project
2. Select the Target
3. right-click and “Add … New Build Phase … New Run Script Build Phase”
4. Copy/paste the script below into the box

ALSO note: it doesn’t matter whether you select “Simulator” or “Device” in the build settings – this script automatically RE-builds the other one for you, and puts everything together in a single binary :)

Finding the library

Read the output from the Build (you may have to select “All messages” in Xcode 4, or it will just say “success!”) – the last line tells you exactly where to find your library.

Script

I’d recommend doing a copy/paste directly from the StackOverflow page – that’s where I will update it with any improvements in future. However, in case that page ever disappears, here’s the latest version as of March 2011:

# Version 2.1
# Changes:
#    - Added Carlos fixes for storing multiple Xcode Projects in a single folder (although you're brave if you do that - Apple doesn't support it well!)
# 
# Purpose:
#   Create a static library for iPhone from within XCode
#   Because Apple staff DELIBERATELY broke Xcode to make this impossible from the GUI (Xcode 3.2.3 specifically states this in the Release notes!)
#   ...no, I don't understand why they did this!
#
# Author: Adam Martin - http://twitter.com/redglassesapps
# Based on: original script from Eonil (main changes: Eonil's script WILL NOT WORK in Xcode GUI - it WILL CRASH YOUR COMPUTER)
#
# More info: see this Stack Overflow question: http://stackoverflow.com/questions/3520977/build-fat-static-library-device-simulator-using-xcode-and-sdk-4

#################[ Tests: helps workaround any future bugs in Xcode ]########
#
DEBUG_THIS_SCRIPT="false"

if [ $DEBUG_THIS_SCRIPT = "true" ]
then
echo "########### TESTS #############"
echo "Use the following variables when debugging this script; note that they may change on recursions"
echo "BUILD_DIR = $BUILD_DIR"
echo "BUILD_ROOT = $BUILD_ROOT"
echo "CONFIGURATION_BUILD_DIR = $CONFIGURATION_BUILD_DIR"
echo "BUILT_PRODUCTS_DIR = $BUILT_PRODUCTS_DIR"
echo "CONFIGURATION_TEMP_DIR = $CONFIGURATION_TEMP_DIR"
echo "TARGET_BUILD_DIR = $TARGET_BUILD_DIR"
fi

#####################[ part 1 ]##################
# First, work out the BASESDK version number (NB: Apple ought to report this, but they hide it)
#    (incidental: searching for substrings in sh is a nightmare! Sob)

SDK_VERSION=$(echo ${SDK_NAME} | grep -o '.\{3\}$')

# Next, work out if we're in SIM or DEVICE

if [ ${PLATFORM_NAME} = "iphonesimulator" ]
then
OTHER_SDK_TO_BUILD=iphoneos${SDK_VERSION}
else
OTHER_SDK_TO_BUILD=iphonesimulator${SDK_VERSION}
fi

echo "XCode has selected SDK: ${PLATFORM_NAME} with version: ${SDK_VERSION} (although back-targetting: ${IPHONEOS_DEPLOYMENT_TARGET})"
echo "...therefore, OTHER_SDK_TO_BUILD = ${OTHER_SDK_TO_BUILD}"
#
#####################[ end of part 1 ]##################

#####################[ part 2 ]##################
#
# IF this is the original invocation, invoke WHATEVER other builds are required
#
# Xcode is already building ONE target...
#
# ...but this is a LIBRARY, so Apple is wrong to set it to build just one.
# ...we need to build ALL targets
# ...we MUST NOT re-build the target that is ALREADY being built: Xcode WILL CRASH YOUR COMPUTER if you try this (infinite recursion!)
#
#
# So: build ONLY the missing platforms/configurations.

if [ "true" == ${ALREADYINVOKED:-false} ]
then
echo "RECURSION: I am NOT the root invocation, so I'm NOT going to recurse"
else
# CRITICAL:
# Prevent infinite recursion (Xcode sucks)
export ALREADYINVOKED="true"

echo "RECURSION: I am the root ... recursing all missing build targets NOW..."
echo "RECURSION: ...about to invoke: xcodebuild -configuration \"${CONFIGURATION}\" -target \"${TARGET_NAME}\" -sdk \"${OTHER_SDK_TO_BUILD}\" ${ACTION} RUN_CLANG_STATIC_ANALYZER=NO"
xcodebuild -project “${PROJECT_FILE_PATH} -configuration "${CONFIGURATION}" -target "${TARGET_NAME}" -sdk "${OTHER_SDK_TO_BUILD}" ${ACTION} RUN_CLANG_STATIC_ANALYZER=NO BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}"

ACTION="build"

#Merge all platform binaries as a fat binary for each configurations.

# Calculate where the (multiple) built files are coming from:
CURRENTCONFIG_DEVICE_DIR=${SYMROOT}/${CONFIGURATION}-iphoneos
CURRENTCONFIG_SIMULATOR_DIR=${SYMROOT}/${CONFIGURATION}-iphonesimulator

echo "Taking device build from: ${CURRENTCONFIG_DEVICE_DIR}"
echo "Taking simulator build from: ${CURRENTCONFIG_SIMULATOR_DIR}"

CREATING_UNIVERSAL_DIR=${SYMROOT}/${CONFIGURATION}-universal
echo "...I will output a universal build to: ${CREATING_UNIVERSAL_DIR}"

# ... remove the products of previous runs of this script
#      NB: this directory is ONLY created by this script - it should be safe to delete!

# Carlos Aragones change: delete just the generated executable, not the whole directory
# NOTE: UNTESTED
#
mkdir -p “${CREATING_UNIVERSAL_DIR}”
rm -rf “${CREATING_UNIVERSAL_DIR}/${EXECUTABLE_NAME}”

#
echo "lipo: for current configuration (${CONFIGURATION}) creating output file: ${CREATING_UNIVERSAL_DIR}/${EXECUTABLE_NAME}"
lipo -create -output "${CREATING_UNIVERSAL_DIR}/${EXECUTABLE_NAME}" "${CURRENTCONFIG_DEVICE_DIR}/${EXECUTABLE_NAME}" "${CURRENTCONFIG_SIMULATOR_DIR}/${EXECUTABLE_NAME}"

#########
#
# Added: StackOverflow suggestion to also copy "include" files
#    (untested, but should work OK)
#
if [ -d "${CURRENTCONFIG_DEVICE_DIR}/usr/local/include" ]
then
mkdir -p "${CREATING_UNIVERSAL_DIR}/usr/local/include"
cp "${CURRENTCONFIG_DEVICE_DIR}/usr/local/include/*" "${CREATING_UNIVERSAL_DIR}/usr/local/include"
fi
fi

Xcode 4: links to help you adapt to the new version

Start here: the “with pictures”, fast and honest, overview of: what’s changed, which bits you care about, and why:

http://fireballed.org/linked/2011/03/09/xcode-pilkington/

Then, when you have more time, try Apple’s official Transition Guide. Unfortunately, Apple doesn’t belive in the World Wide Web, so the URL keeps changing, and they don’t update the old URLs, they just delete the contents. So, this link may well stop working:

HTML: http://developer.apple.com/library/mac/documentation/IDEs/Conceptual/Xcode4TransitionGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40009984-CH1-SW1
PDF: http://developer.apple.com/library/mac/documentation/IDEs/Conceptual/Xcode4TransitionGuide/Xcode4TransitionGuide.pdf

Apple’s Chief Sadistic Officer decided to change many of the shortcuts – and in some cases coded them so you can’t put them back again. Before you go nuts and have a breakdown, here’s a complete list:

http://cocoasamurai.blogspot.com/2011/03/xcode-4-keyboard-shortcuts-now.html

(and the single most annoying one – the key combo that most professional programmers use thousands of times a day – appears to be accidentally hardcoded. We’re hoping this will get fixed VERY soon! (stackoverflow: switch between header file and source file))

Frameworks … have changed completely. The most important thing is “how the heck do I Add Framework now???”:

http://stackoverflow.com/questions/3352664/how-to-add-existing-frameworks-in-xcode-4

Making custom templates for Xcode 4 – March 2011

UPDATED 2012: Apple broke it all AGAIN (again). New locations below

Xcode 4 is here – yay!

Apple’s templates for Xcode are still poor, inserting C-style comments and VERY annoying fake “copyright” messages in every file – boo!

Xcode 4 has a massively improved templates system – yay!

No-one knows how it works – … oh.

So, after a load of trial and error, here’s a step-by-step guide. If I find better docs from Apple on how to do this, I’ll add a link HERE. (not found them yet, though).

Before we start, some observations:

  1. Apple has changed the locations of EVERYTHING
  2. Apple has changed the format of EVERYTHING
  3. Whenever you upgrade Xcode, if you didn’t take precautions up-front, Apple will DELETE ALL your templates
    • (this is a feature, not a bug – just happens to be a very annoying feature – it’s a pity Apple didn’t include a “WARNING: About to delete your custom settings! Are you sure?” dialog in the Xcode installer)

0. DO NOT blindly read the links on Google for this topic

Why?

  1. Apple has made incompatible changes to the Xcode 3 templates system, so all the “old” articles no longer work
  2. …but they also changed the Xcode 4 system just before launching, so that most of the “new” articles don’t work either!

Um…? Yeah. Apple always does this. It’s an improvement (reading the Google links, the Xcode 4 beta process was a bit harder to follow), but it’s still a PITA that all the tutorials are now broken.

1. Grab the existing templates, and “convert” them to “personal” templates

Why? Apple will NOT delete “personal” templates during Xcode upgrades

How?

  1. 2012: Find your copy of Xcode 4 (the app itself – usually in Applications), and right click “Show Package Contents”
  2. 2012: Navigate in there until you find the “Templates” sub folder. There are two folders, one for Mac, the other for iPhone
    • Mac location: [Xcode4.app]/Contents/Developer/Library/Xcode/Templates

    • iPhone location: [Xcode4.app]/Contents/Developer/Platforms/iPhoneOS.Platform/Developer/Library/Xcode/Templates
  3. (previously: Go to /Developer/Library/Xcode )
  4. 2012: Un-hide your Library folder (google if you don’t know how, or else do this from Terminal)
  5. Go to ~/Library/Developer/Xcode
  6. Copy/Paste ONE OF the “Templates” folders into this folder
  7. 2012:
    • Go into the “File Templates” folder, and RENAME EVERY SUB-FOLDER
    • (if you do not do this, as of 2012, Apple will DELETE/IGNORE your templates. In previous versions, they’d at least honour the File Templates)
  8. 2012: … if you want *both* OS X *and* iOS custom templates, repeat the above with the other “Templates” folder

(REMOVED: all the text here about testing the above – Apple broke most of it, so it’s not worth using any more)

2. Re-categorize your templates so you can find them in Xcode GUI

We’ll start with File Templates…

  1. The File Templates are all stored in “File Templates/”
  2. When you do “new file” Xcode displays one entry in the sidebar for each sub-folder of “File Templates/”
  3. When you select a sidebar item, Xcode looks in that subfolder, and creates one icon for every sub-subfolder whose name ends in “.xctemplate”

How?

  1. Pick a sub-folder you want to customize, and rename it
  2. 2012: you’ve already had to rename it … previous versions allowed you to keep the names the same, Apple now prevents this
  3. e.g. I renamed “Cocoa” to “Adam’s Cocoa”

Test?

  1. Try creating a new file in Xcode
  2. …notice that you have a new sidebar entry
  3. ……which contains all the normal templates from the Cocoa section

3. Edit your templates to your heart’s content

The new template system is a LOT better than the old one. The old one was a nice idea (it could be opened directly inside Xcode), but … it had a lot of bugs. Try changing the NIB files in Xcode3, for instance (as soon as you save one, Xcode screws up the auto-filename generation for that template).

There’s two things you have to edit, separately (and they SHOULD be separate! This is a good thing!)

How?

  1. The “TemplateInfo.plist” file contains all the options for the template, and lets you build custom GUI questions that’ll appear in the New File wizard
  2. If your TemplateInfo.plist file lists “optional” versions of the template, with different sets of files, Xcode will look for subfolders named after the option that the user chose, and use ONLY the files in the one that matches
  3. ALL OTHER FILES in the .xctemplate folder will be copied directly across to the new project

So, to add a new file that applies to your template ALWAYS:

  1. place it inside the .xctemplate folder

To add files that only apply to your template SOMETIMES, depending on what options the user chose:

  1. invent an internal name for the option
  2. create a subfolder of .xctemplate folder, give it that internal name
  3. edit your TemplateInfo.plist file and add an Options section that allows the user to select this option (see below)

To add an Options section

  1. I’d recommend copy/pasting the one from : “File Templates/Cocoa/Objective-C class.xctemplate/TemplateInfo.plist
    1. note how there 5 options, and 5 subfolders with the same name

Google App Design vs. Apple App Design

A wonderful example of how Google and Apple (seem to) approach design fundamentally differently – and of why Google has failed so heavily with Android handsets – courtesy of TechCrunch:

Wow, Google Has Ported My Ten Thousand Button Nightmare To The iPhone!

Don’t get me wrong – Android itself is doing very nicely indeed (sales figures approaching parity with iPhone / iPod Touch / iPad) – but … remember how Google used to sell phones, only a year ago?

Remember how the Nexus One was, supposedly, the first of many?

Hmm.

A lot went wrong. I met some of the Google staffers from the handset project around the time, and listened to them talk about what made their phone great. They kept going on about technical features such as “voice control is amazing!”, while ignoring the practicalities (e.g. at the first demo I saw, they couldn’t get a good enough signal, so the voice control kept failing – every single time. Took them 3 minutes to do what you’d have done in 10 seconds just by typing).

All through, it seemed Google employees couldn’t conceptualise the idea of a product being appropriate for the user.

They were full of words like “simple”, “easy”, “fast” – but refused to put them into the context of a real person. They never stopped to think: “in theory, voice may be “fast”, but in practice: is it?”.

And so … looking at the Google Remote app referenced in the TC article … I’d imagine they thought to themselves they were being clever: they’ve taken a real-world paradigm, and cloned it. It should be “familiar to ordinary people”, it has “every button you need”, etc.

But they probably never stopped to ask themselves: does anyone actually WANT it to work this way?

Download Xcode4 – without crashing / restarting

Apple.com uses some custom webserver / server hardware that doesn’t really cope with the popularity of a large site like Apple.

Normally, this is just a minor irritation. However, each time they release a new version of Xcode and the iOS SDK, Apple requires you to re-download everything – even if you already have 90% of it. So, the filesize of Xcode 4 (which is now required for iOS dev) is 4.3 gigabytes.

That’s enormous.

And, unfortunately, due to the way Apple’s webservers are configured, it WILL deliberately crash-out halfway through download if you don’t manage to download it “soon enough” (by Apple’s arbitrary standards). This is worse at the moment because a lot of developers are downloading it, and we’ve noticed that download speeds from Apple.com have been slower than normal (probably our ISP – I’m sure Apple has enough bandwidth, but *your* ISP may be seeing the increased traffic and throttling it).

You can end up wasting days just trying to get it to complete before Apple logs you out. Here’s a workaround for Firefox:

  1. Login to Apple’s developer site
  2. Start downloading XCode

…if it finishes first time (and check the final file size – should be over 4 GB): congrats. You’re done!

If not, you can try hitting “pause” and “resume” in Firefox download window – but normally what’s happened is that Apple’s webserver has told Firefox to stop, and refuses to let it resume. They’ve reason for doing this, but there are much better / safer ways they could have handled it.

Anyway, here’s the workaround:

  1. Hit cmd-j in firefox (brings up the downloads window)
  2. Select the crashed download, and right click “Reveal in Finder”
  3. You should see that there’s a file with the same name but “.part” added on the end – this will be how much you downloaded
  4. UNFORTUNATELY: Apple’s webserver will specifically tell Firefox NOT to resume downloading the file, and so your progress is lost.
  5. UNLESS:
    1. you select the “.part” file
    2. copy/paste it into the same directory (it’ll now be called “(previous name).part copy”
    3. restart the download in Firefox (note: it’ll start agian from 0 bytes :( )
    4. pause the download
    5. delete the “new” .part file
    6. rename the old .part, removing the text ” copy” off the end
    7. resume the download

…and this time, Firefox won’t complain, it’ll resume from where it left off.

How to make Angry Birds: 51 failures and years of experiments

I thought this was interesting reading for anyone looking at App Store success / failure. All the obvious stuff is in there towards the end (e.g. things about working with your fanbase, answering every email – even leveraging non-english-language stores to get enough momentum that publishers will even talk to you, etc) – but to me the unusual points are more about how many failures they were *expecting* in order to get their success:

http://www.wired.co.uk/magazine/archive/2011/04/features/how-rovio-made-angry-birds-a-winner?page=all

“First they had to save a company in crisis: at the beginning of 2009, Rovio was close to bankruptcy. Then they had to create the perfect game, do every other little thing exactly right, and keep on doing it. The Heds had developed 51 titles before Angry Birds. Some of them had sold in the millions for third parties such as Namco and EA, so they decided to create their own, original intellectual property. “We thought we would need to do ten to 15 titles until we got the right one,” says 30-year-old Niklas.”

“When Mikael rejoined the company at the beginning of 2009, he and Niklas sat down to work out a rescue plan. The app store was the integral part. They would continue to work for hire to make ends meet, but at the same time develop their own iPhone games, abandoning other platforms. “The iPhone was a hyper-competitive environment,” says Mikael. “If we succeed there, we can go to other smartphones. And if we do well there, we can go to PC and console, and beyond.”

“Mikael dedicated a budget of €25,000 (the final cost would be four times that) and the team worked on it as a hobby project: “Whenever we had slack time we would do this,” says Niklas. During the six months that the team worked on the game, they produced another four games for other companies. Rovio continued to refine the game into the winter.”

” Angry Birds was Rovio’s 52nd title.”