Renaming Variables in Xcode

For both persons and programming variables, a good name is an invitation to a prosperous life. Luckily for your Objective-C variables, changing their name is easier than the legal trouble of changing your own name to ‘Max Power’.

Find out how:

‘Edit All In Scope’

To rename a symbol present solely inside a function, use the ‘Edit All In Scope’ function. As it the name implies, this will edit the symbol’s name inside the current scope.

To make use of this feature:

  1. Place the cursor on top of the symbol to be renamed and press CTRL+E.
  2. Edit your symbol’s name.
  3. Press Enter to finish the edit.

Alternatively, you can try placing your mouse on top of a symbol, wait until it is underlined, select the menu ‘Edit All In Scope’ and go on from there.

Personally, I found this approach annoying.

Xcode’s Refactor

If the symbol you intend to rename is a property of a class, a constant used across your project or any other symbol used in any scope wider than a function or method, you should use Xcode’s Refactor instead:

  1. Left-click the symbol to be renamed.
  2. Select Refactor -> Rename from the contextual menu.
  3. Finally, give that poor variable a name it can be proud of.

    If you selected the ‘Rename related files’ option, Xcode will look for other files using the variable and rename it there but, this method is not fail safe — keep reading!

Avoid Breaking Stuff: NSStringFromSelector

  1. The first strategy is not to trust Xcode’s refactor by a 100% and run a project-wide search for the soon-to-be-replaced identifier. Look particularly for occurrences inside strings passed to NSPredicates or used in KVO. It’s pretty easy to miss those ones just to find them later as unexpected NSUndefinedKeyExceptions. Not good…
  2. To help Xcode’s refactor tools, use the function NSStringFromSelector() every time you make reference to an object’s property (instead of simply typing it inside a string)
    self.name = [aDecoder decodeObjectForKey:@"name"];
    

    NSStringFromSelector gives Xcode a little bit of context on what that particular string means and a hint to include that occurrence in case of refactoring.

    self.name = [aDecoder decodeObjectForKey:NSStringFromSelector(@selector(name))];
    

Pebble Smart-watch, Xcode 5 and OS X Mavericks

After stumbling a few times while setting up my development environment for the Pebble Smart-watch, I decided to write a step-by-step guide on the topic to save time and sanity to other developers coding their first application for Pebble using Xcode 5 under OS X Mavericks and iOS.

Things You’ll Will Need In Your iOS Device

  1. Update to iOS 7 just to be sure.
  2. Don’t bother downloading the Pebble app from the App Store (v1.x). You need to submit a form to receive an .ipa with the 2.x version.
  3. After receiving the .ipa file with the Pebble app v2.x, install it. Open your device’s Settings app, select Pebble and enable ‘Developer Mode’.
  4. Return to the home screen and open the Pebble app. Select ‘Developer Connection’ and enable it.

Things You Need In Your Pebble Smart-Watch

  1. Be sure you have opened the iOS Pebble app v2.x at least once.
  2. Put your Pebble in Recovery Mode: Press the Back button, the Up button and the Select button for at least 30 seconds until you see ‘Loading…’ on the screen.

    Don’t let that ‘Pebble’ screen fool you! Keep pressing those buttons until you can read ‘Loading…’.

  3. Re-pair your Pebble with your iOS device’s bluetooth.
  4. If your device suggests that an upgrade is available, refuse the upgrade.
  5. Open one of the following links from your iOS device, your phone will start upgrading your Pebble:

    If your serial number starts with a number, download Pebble Firmware 2.0-BETA2 for ev2_4..

    If your serial number starts with the letter Q, download Pebble Firmware 2.0-BETA2 for v1_5.

Things You Need In Your Computer

  1. Install the Xcode Command Line Tools by typing the following command in the Terminal:
    xcode-select --install
    

    If you have problems installing them via this command (‘Can’t install the software because it is not currently available from the Software Update server’ error), download the installer manually from the Developer Center.

    Don’t skip this step! The Command Line Tools are not installed by default in Xcode 5.

  2. Download the latest version of the Pebble SDK. Go and grab it from the Getting Started Guide, there is big orange button that reads ‘PEBBLE SDK’.
  3. Create a directory for all Pebble tools:
    mkdir ~/pebble-dev/
    
  4. Unzip the Pebble SDK inside that directory. You should end with something like: ~/pebble-dev/PebbleSDK-2.0-BETA2.
  5. Add the Pebble tool to your command line:
    echo 'export PATH=~/pebble-dev/PebbleSDK-2.0-BETA2/bin:$PATH' >> ~/.bash_profile
    
  6. Reload your shell configuration to make it available immediately
    source ~/.bash_profile
    
  7. Download the toolchain for OS X (arm-cs-tools-macos-universal-static.tar.gz).
  8. Extract that compressed file inside ~/pebble-dev/PebbleSDK-2.0-BETA2. Be sure you end with something like: ~/pebble-dev/PebbleSDK-2.0-BETA2/arm-cs-tools.
  9. Install the Python packet manager, pip:
    sudo easy_install pip
    
  10. And all the Python dependencies used by Pebble:
     pip install --user -r ~/pebble-dev/PebbleSDK-2.0-BETA2/requirements.txt
    

    If this last step fails, it is probably because you don’t have the Xcode Command Line Tools installed.

Things You Need In Your Xcode Project

  1. Include PebbleKit.framework and PebbleVendor.framework in your Xcode project.
  2. Link ExternalAccessory.framework, libz.dylib, CoreBluetooth.framework, CoreMotion.framework, CFNetwork.framework and MessageUI.framework.
  3. Add -ObjC to Other Linker Flags of your target’s Build Settings.
  4. In your Info.plist file, add the value com.getpebble.public to the Supported external accessory protocols array.
  5. Also in the Info.plist file, add the value App communicates with an accessory to the Required background modes array.
  6. In the Build Settings of your project, change Architecture’s value from $(ARCHS_STANDARD_INCLUDING_64_BIT) to $(ARCHS_STANDARD).

    Otherwise, you will bump with the following error:

    Undefined symbols for architecture x86_64:
      "_OBJC_CLASS_$_PBPebbleCentral", referenced from:
          objc-class-ref in NPViewController.o
    ld: symbol(s) not found for architecture x86_64
    

After all those steps, your watch, Mac, iPhone and Xcode should be ready to start coding.

Did somebody said ‘Hello World‘?

Happy coding!

Tweeting Developers

For a long time I saw Twitter as a source of mild entertainment which occasionally brought me up to update on something relevant.

But that point of view changed recently, when I cherry-picked who I’m following and discovered the interaction I can have with some great developers.

In no time it became a valuable tool that updates me on what’s happening in my tech niche, tips me about new interesting things to learn and puts me in touch with other cool people doing cool stuff.

But don’t take my word for granted. Leif Singer (@lsinger), a mad scientist from the University of Victoria in Canada, made a thorough research on How Software Developers Use Twitter.

He wrote a great post about it on his blog that I just can’t recommend enough. It includes a tl;dr and usage strategies for developers using Twitter. Pure gold.

Check it out. You know, for science!

Command that Terminal

In case somebody else is using the left and right arrows to navigate through typed commands, I’ve compiled a list of useful shortcuts for the Terminal:

  • Ctrl + A: Go to the beginning of the line.
  • Ctrl + E: Go to the end of line.
  • Ctrl + F: Move the cursor position one space forward.
  • Ctrl + B: Move the cursor position one space backward.
  • Alt + F: Move the cursor position one word forward.
  • Alt + B: Move the cursor position one word backward.
  • Ctrl + U: Clear the line before the cursor.
  • Ctrl + K: Clear the line after the cursor.
  • Alt + Del: Delete the word before the cursor.
  • Alt + D: Delete the word after the cursor.
  • Tab: Auto-complete file and directory names.
  • Ctrl + L: Clears the screen (same result as typing the clear command).
  • Ctrl + C: Terminate the running program; a timeless classic.

Edit: As noted by Parker Wightman (@parkerwightman), these same key bindings work in nearly every OSX text field.

As I grow older I found myself more and more comfortable working from the OSX Terminal, but I was annoyed by the “missing commands to navigate through the text”… Until I discovered them recently and my mind was blown.

Hope they are useful for somebody else too.