5 More Tips and Tricks

The last few posts have been pretty long and drawn out, so this time we’ll make a short and sweet post that helps save you some time. Building on the past tips’n’tricks article, here’s a few more.

Assumptions

Use Pre compiler Directives for DEBUG specific code

When creating Objective-C code, most developers make extensive user of NSLog to print out information and help fix crashing. The problem is, NS

Log code should not really be included in a production app. An easy solution is to user compiler directives that will removed when building anything that is not a “DEBUG” build.

As an example you could define all your NSLogs like so:


#ifndef DEBUG
NSLog(@"Log some data");
#endif

But, no one really wants to type all that junk every time they need an NSLog, so a better way is to create a definition in a header file.


#ifdef DEBUG
#   define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#else
#   define DLog(...)    
#endif

this code snipped allows you to call ‘DLog’ instead of ‘NSLog’ and it will contain your method and line number as well as your log function. Simply add it to a header file and include it wherever you want to log some stuff.

Example:


#import someFileWithCodeSnippet.h
//...

//...
DLog(@"whatever you want")

If you want to get even more fancy you can include this chunk of code in your .pch file. Which incidentally is the next time saving tip on our list.

Import very common definitions using a .pch file.

Ok, first, as a disclaimer, this only works in Objective-C as it’s use is generally frowned upon for most things. If you need to import classes in most of your classes, consider making a base class or a category (more on that in a moment). I think the Debug DLog definition is something that is pretty acceptable to add to a .pch file however.

In Xcode 5, the .pch file is usually already added in the supporting files. simply open it up and add the code shown above.

In Xcode 6. You have to make your own .pch file (Objective-C Only). The steps for this are as follows:

  1. Make a New File (The ‘+’ button in the bottom left corner, or ⌘ + N)
  2. iOS ➔ PCH File ➔ NameItWhateverYouWant.pch
  3. GOTO: Project ➔ Build Settings and Search: “Prefix Header
  4. Under “APPLE LLVM 6.0” you will see a row called “Prefix Header”
  5. Type in: “YourProjectName/YourPrefixFileName.pch”
  6. Clean and Build/Run!

Figure 1 Shows this process.

Figure1

Figure 1: Making a .PCH file in Xcode 6

Use Categories To Make Your Life Easier

Often times as a developer , you’ll receive a design spec that will include font styling, colors, and images. Personally, I like to keep these design element in one consolidated area. I typically will do this using a category. A Category lets you add methods to an existing class. So in a project I might have a category for the following classes:

UIColor
UILabel
UIFont
UIImageView

Clear as mud? Here’s a quick example.

First create a category by creating a new file by pressing “+” in the bottom left corner of the project navigator. You can also press “⌘ + N”. When the dialog pops up, choose “Objective-C File” and press “Next”.

In the next dialogue choose “Category” under File Type and type in “UIColor” for Class. You Can name it whatever you want, but typically I’ll name this my project prefix + “UIColor”. For this Example TATColor. Press Next and choose a place to save your file.

Once the files are made you’ll notice they look a little different from most classes. If you used TATColor as your file name the files will have a name of “UIColor + TATColor.h/.m”. This lets you know these files are extensions.

Now that we’ve made an extension, let’s use it! A designer might give you a bunch of colors, say the ones found here: http://designmodo.github.io/Flat-UI/

On this site there are a bunch of colors with interesting names. So we’ll map them to our color class.

The first thing we’ll want to do is create a method or definition to convert RGB into something Objective-C can handle. I like the concise nature of a macro, so I’ll use that.

in the TATColor + UIColor.h file add the following code shown in Code Chunk 1


1
2
3
4
5
6
7
#import <UIKit/UIKit.h>

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue &amp; 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

@interface UIColor (TATColor)

@end

this neat little macro allows us to define our colors with

UIColorFromRGB(0xFFFFFF)

.

Now that we have our macro setup let’s create some class methods that will conveniently return the colors we want. Code Chunk 2 shows the modifications to the .h and .m files.

TATColor + UIColor.h


#import <UIKit/UIKit.h>

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

@interface UIColor (TATColor)

+ (UIColor *)TATTurquiseColor;
+ (UIColor *)TATEmeraldColor;
+ (UIColor *)TATPeterRiverColor;
+ (UIColor *)TATAmethystColor;
+ (UIColor *)TATWetAsphaltColor;


@end

TATColor + UIColor.m


#import "UIColor+TATColor.h"

@implementation UIColor (TATColor)

+ (UIColor *)TATTurquiseColor{
    return UIColorFromRGB(0x1ABC9C);
}
+ (UIColor *)TATEmeraldColor{
    return UIColorFromRGB(0x2ECC71);
}
+ (UIColor *)TATPeterRiverColor{
    return UIColorFromRGB(0x3498DB);
}
+ (UIColor *)TATAmethystColor{
    return UIColorFromRGB(0x9B59B6);
}
+ (UIColor *)TATWetAsphaltColor{
    return UIColorFromRGB(0x34495E);
}

@end

You’ll notice that I deviate from standard camel case and prefix all of my colors with “TAT”. I do this so I can easily tell system colors from specific project colors. This is matter of prefference and I’m sure someone will tell me I’m a bad person for doing it.

Now, for the true benefit of this category. Next time you are coding away and you need a color, you can simply get the color by typing

[UIColor TATTurquiseColor]

. Take Code Chunk 3 for instance.


tmpLabel.textColor = [UIColor TATTurquiseColor];

You don’t have to use much imagination to see how this could benefit you. Plus, if you need to slightly adjust the color you can change all of them from one place.

Use Alcatraz for Xcode

We all like plugins right? Well, You may not have known it, but there are many plugins available for Xcode. Even better, there’s a neat system called “Alcatraz” for managing plugins easily. With Alcatraz you can one click install a plugin that will better format warnings or automatically format your code. Enough has been said on the subject and I don’t feel like repeating it here. So I’ll leave you with two good links:

  1. Install Alcatraz from directions found here: Alcatraz.io
  2. Have a look at some useful plugins courtesy of NSHipster: NSHipster.com/Xcode-plugins

Have You Seen The View Debugger?

In Xcode 6, Apple gave us a nice little feature for viewing the construction of our view hierarchies in real time. Simply run your app and press the button shown in Figure 2 to open it up.

Figure2

Figure 2: The View Debugger button

Once you do , you can rotate your views in a 3D orientation, expand and contract layers, and hide layers from the front or back of the view hierarchy. Figure 3 shows what this looks like.

Figure3

Figure 3: The View Debugger in action

The only caveat to the view debugger is it only seems to work on iOS 8 simulations only.

That’s it for this post, Hope you enjoyed! As always, I welcome any feedback and will try to answer questions as I see them.

About Joe Hoffman

I am An Electrical Engineer that spends a lot of my off time doing web development and other programming. Currently I am trying to expand my knowledge with iOS and I find that writing tutorials helps me learn more thoroughly as well as provide some useful info to others.

Speak Your Mind

*

css.php
Privacy Policy