Find Me
Tweets
Thursday
Oct062011

Goodbye, Steve

I didn’t become an Apple user until a few years ago. I came to the platform out of curiosity. I bought myself a Macbook to start teaching myself how to code iOS apps. I quickly found myself amazed at the ingenuity and simplicity of the entire platform. It was the way everything tied together, and always seemed to work in the way you expected. The more I lived in the Apple world, the more impressed I became. In 3 years, I have gone from using Apple’s products as a hobby to a full time developer on their platform. For the first time in my life, I have questioned that someone actually pays me to work on this stuff. I guess if you ask yourself that question, you are doing the right thing with your life.

I was surprised and saddened to hear of Steve Jobs’ passing tonight. He truly made a difference in our world with sheer will and by questioning everything. His stamp is all over these products we all use every day for entertainment and for work. His drive and determination produced these incredible products and services which set the standard for excellence in technology.

A quote out of the recent statement by President Obama on Steve Jobs’ passing struck me as particularly apt:

“The world has lost a visionary. And there may be no greater tribute to Steve’s success than the fact that much of the world learned of his passing on a device he invented.”

I was glad to hear he passed peacefully and with his family.

Rest in peace, Steve. Our world is a little less without you in it.

Thursday
Sep292011

Quick Xcode 4 Tip: Deleting Derived Data to Rescue Code Completion

Quick Tip:

I had an issue on a very large project in XCode. Starting 2 days ago, all of my syntax highlighting was completely broken. This is bad, but even worse, my code completion worked only partially. I was getting autocomplete for local variables only. When calling framework methods with a lot of parameters, I rely pretty heavily on code completion. I tried all of the usual fixes. This includes:

  1. Doing a Project Clean (⌘⇧K)
  2. Doing a Clean Build Folder (⌥⌘⇧K)
  3. Restarting XCode

None of these solutions fixed the issue. After some digging, I came across this StackOverflow Post which pointed me in the right direction. Seems like sometimes the project index, which contains metadata used to display code completion suggestions (among other things), can become corrupted. The solution is to delete it. This will force XCode to regenerate it, which rebuilds the index.

Here are the steps to delete your Derived Data folder.

  1. Go to XCode Settings (⌘,)

  2. Click the Locations tab. You will see a screen like this:

  3. Locations 1
  4. Click the small arrow next to the location of the Derived Data folder. This will open the Finder to the location of the Derived Data for any XCode projects you currently have on your Mac.

  5. Find the one that starts with the name of your current project and delete it.

  6. You should immediately see XCode start to re-index your project

For me, after this process completed, I got all of my functionality back. Your mileage may vary, and please make sure you back everything up before deleting anything!

Monday
Aug082011

BackBlaze, MDS Worker, and Spotlight

I recently began experiencing my system fans kicking in and running all the time. The fans began running so much that I almost didn’t notice them any more. When I pulled the Activity Monitor up, the md worker was using over 100% of my system resources consistently.

After looking up what md worker does, it appears to be a process spun off by spotlight to index your drive for quick searching. Even after a reboot, the fans would kick back in after a short idle time of maybe a minute. This process was seriously heating up the mac.

After doing some more research, I found this excellent question and answer from the SuperUser site. I ran the following command in the terminal to log what Spotlight was trying on index at the time.

sudo fs_usage -w -f filesys mdworker

The terminal showed the last file to be indexed was in the /Library/BackBlaze/bzdata folder. This was not Library in ~/Users, but the one at the base of the volume. It was indexing this file over and over. My initial thought was that the system was hung on the file. A google search turned up this result on the BackBlaze support page. BackBlaze frequently writes log data to a file. All files in the BackBlaze folder should be added to the Privacy exclusions so that Spotlight will not attempt to index them. According to the support article, this exclusion should be added when BackBlaze is installed. Since the Lion version for BackBlaze was very recently released, this may be a bug in the BackBlaze installer.

Help mac spotlight

In my case, the folder had not been added.

After adding the BackBlaze folder to the Spotlight Privacy exclusions, the problem disappeared at once. Good job on BackBlaze for having an excellent support document.

 

UPDATE: I recently had to replace my hard drive and decided to do a fresh install rather than pulling over my Carbon Copy Cloner backup. I experienced the exact same problem, and had to follow the process outlined above to fix it. This issue hasn't been resolved in the BackBlaze installer as of yet.

Friday
Jun102011

On Discovering New APIs

In working with any programming language, I've always found that if you seem to be fighting things to get work done, you're probably doing it wrong. This is especially true in Cocoa Touch. When i was first learning the frameworks, I read a lot of blogs, books, and any other materials I could get my hands on. ALmost everyone said something to the effect of "You don't get it? Read the docs again."

It took me a while to realize that Apple has excellent documentation. It's gotten a lot better in the Microsoft world, but in the C# 2.0 and 3.5 days, you would frequently go in search of a particular framework method or property, and come across a blank page that was obviously generated by some kind of tool. Or, my favorite was when you had a property that was named something like ToolsArray, and the description would contain verbiage like "The array that holds the tools".

Apple has excellent documentation that gives common sense user cases, detailed definitions, and, in most cases, real world usage examples. I was reading the excellent new book iOS Recipes by Matt Drance and Paul Warren, and discovered a new gem. This one is on the UserDefaults class.

I use the UserDefaults class in a few places in my apps, mostly to hold and maintain state, such as if the app has been launched, if the user has been registered, etc. I always found myself writing guard code like this:


BOOL appWasLoaded = NO;
NSUserDefaults *standardDefaults = [NSUserDefaults standardDefaults];
if([defaults objectForKey:@"appLoadedKey"]{
    appWasLoaded = [defaults boolForKey:@"appLoadedKey"];
}

There is a way to ensure user defaults have some pre-configured settings, which will reduce some of the redundancy in testing that user settings key/value pairs exist. This is accomplished using the following method call on the user defaults:


[[NSUserDefaults standardUserDefaults] registerDefaults:defaults];

When you make this method call, the defaults variable is an NSDictionary that contains they keys and values you want to set as initial defaults. The best part about this particular method, though, is that it will only set the values in user defaults if they don't already exist. What I have been doing to set these initial defaults is to create a .plist file that includes my default values. I pass this into the code as a dictionary, then call the registerDefaults method on startup to provide the default values at runtime. Here is a quick example:

My PList looks like this:

    
    
    
    
        
            Test
            TestValue
        
    

    NSDictionary *defaultValues = 
        [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"defaultValues" 
                                                                                   ofType:@"plist"]];
    [[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];

After this point, calling

[[NSUserDefaults standardUserDefaults] stringForKey:@"Test"]
will get you the string "TestValue", even if you haven't initialized it somewhere else in your code.

For a full rundown, make sure you check out the official documentation.

Tuesday
Jun072011

Using NSXMLParser

This is my first stab at an iPhone project. I'm writing an app that consumes an RSS feed and parses it to retrieve a few bits of information for display on the screen. This is my first attempt at an implementation. Coming from a .NET background, it is quite a paradigm shift when switching to the heavily convention based world of Cocoa development.

If I was writing this app in .NET, I'd go out looking for the proper class libraries to parse the XML and begin framing out a class to perform all the functions I needed to populate an object to be persisted to the UI.

When researching the best way to do this, I found 2 prevalent methods of parsing XML in Cocoa:

I decided to go with NSXMLParser because it seemed like the path of least friction to get started with. NSXMLParser is a stream based reader. It basically reads the entire XML tree character by character and fires off events via delegate calls when it encounters useful information. I found this model very easy to use. After some trial and error, I've got it working well for my current project.

I am parsing the xml feed for Woot.com. The following is an abbreviated snippet of the elements I am interested in. The actual feed has a lot more info in it, but this should give you a feel for the structure:


    
    Woot! - One Day, One Deal
    http://www.rssboard.org/rss-specification
    Oppositional.Syndication, 1.0.0.0, http://www.oppositionallydefiant.com/
    
      [Removed for brevity]
      http://www.woot.com/
      Dyson DC24 Blueprint Limited Edition
      $279.99
      False
False
      http://www.woot.com/WantOne.aspx?id=e94aa72d-5860-4f9b-bab2-42211de2f4cc
      http://sale.images.woot.com/Dyson_DC24_Blueprint_Limited_EditionbklThumbnail.jpg
      http://sale.images.woot.com/Dyson_DC24_Blueprint_Limited_EditionxxcShoppingYahoo.jpg
      http://sale.images.woot.com/Dyson_DC24_Blueprint_Limited_Editionm32Standard.jpg
      http://sale.images.woot.com/Dyson_DC24_Blueprint_Limited_Edition98lDetail.jpg
      $5 shipping
	
	

Defining the Model Object

The first thing I did was create a model object which would encapsulate the element values from the rss feed that I was interested in. Here is the code for the class:

SaleItem.h

#import 


@interface SaleItem : NSObject {
	NSString *itemTitle;
	NSString *itemPrice;
	NSString *itemThumbnaillUrl;
	NSString *itemSubstandardImageUrl;
	NSString *itemStandardImageUrl;
	NSString *itemDetailImageUrl;
	NSString *itemDescription;
	NSString *itemShippingCost;
	NSString *itemSaleUrl;
	
	BOOL isWootOff;
}

@property (nonatomic, retain) NSString *itemTitle;
@property (nonatomic, retain) NSString *itemPrice;
@property (nonatomic, retain) NSString *itemThumbnaillUrl;
@property (nonatomic, retain) NSString *itemSubstandardImageUrl;
@property (nonatomic, retain) NSString *itemStandardImageUrl;
@property (nonatomic, retain) NSString *itemDetailImageUrl;
@property (nonatomic, retain) NSString *itemDescription;
@property (nonatomic, retain) NSString *itemShippingCost;
@property (nonatomic, retain) NSString *itemSaleUrl;

@property (nonatomic, assign) BOOL isWootOff; 

@end

SaleItem.m

#import "SaleItem.h"


@implementation SaleItem

@synthesize itemTitle;
@synthesize itemPrice;
@synthesize itemThumbnaillUrl;
@synthesize itemSubstandardImageUrl;
@synthesize itemStandardImageUrl;
@synthesize itemDetailImageUrl;
@synthesize itemDescription;
@synthesize itemShippingCost;
@synthesize itemSaleUrl;
@synthesize isWootOff;


-(void) dealloc{
	[itemTitle release];
	[itemPrice release];
	[itemThumbnaillUrl release];
	[itemSubstandardImageUrl release];
	[itemStandardImageUrl release];
	[itemDetailImageUrl release];
	[itemDescription release];
	[itemShippingCost release];
	[itemSaleUrl release];	
	
	[super dealloc];
}

@end

Defining a Class to Reference Constants

Next, to make my life easier, I defined a class to define some constants. These constants define the string values I will be searching for as I parse the XML.

Constants.h

#import 

extern NSString * const ITEM;
extern NSString * const TITLE;
extern NSString * const PRICE;
extern NSString * const THUMBNAIL_IMAGE;
extern NSString * const SUBSTANDARD_IMAGE;
extern NSString * const STANDARD_IMAGE;
extern NSString * const DETAIL_IMAGE;
extern NSString * const DESCRIPTION;
extern NSString * const IS_WOOTOFF;
extern NSString * const SHIPPING_PRICE;
extern NSString * const SALE_URL;

@interface Constants : NSObject {
	
}

Constants.m

#import "Constants.h"
@implementation Constants
	NSString * const ITEM				= @"item";
	NSString * const TITLE				= @"title";
	NSString * const PRICE				= @"woot:price";
	NSString * const THUMBNAIL_IMAGE	= @"woot:thumbnailimage";
	NSString * const SUBSTANDARD_IMAGE	= @"woot:substandardimage";
	NSString * const STANDARD_IMAGE		= @"woot:standardimage";
	NSString * const DETAIL_IMAGE		= @"woot:detailimage";
	NSString * const DESCRIPTION		= @"description";
	NSString * const IS_WOOTOFF			= @"woot:wootoff";
	NSString * const SHIPPING_PRICE		= @"woot:shipping";
	NSString * const SALE_URL			= @"woot:purchaseurl";
@end

Creating the Worker Class

NSXMLParser works using delegate callbacks to let you hook into events that you are interested in. Most of the examples I found online used delegate methods right inside the viewcontroller to accomplish the parsing. Since I have several views which are going to take advantage of this functionality, I decided to split it out into a worker class. Let's construct this class (I named it XMLParser) piece by piece:

XMLParser.h

#import 

@class Constants; //Forward declaration of the Constants class
@class SaleItem;  //Forward declaration of the SaleItem model object class

@interface XMLParser : NSObject {
	NSMutableString *currentValue; //This will hold element values as we build them piece by piece
	BOOL itemElementInProgress;	// This will determine if we are currently parsing an element
	SaleItem *saleItem; // Instance of our model object
}

@property (nonatomic, retain) NSMutableString *currentValue;
@property (nonatomic, retain) SaleItem *saleItem;
@property BOOL itemElementInProgress;

- (BOOL) parse; // This performs the parsing of the file
- (id)initWithSaleItem:(SaleItem *)aSaleItem; // Custom initializer
- (void) trimString; // Small convenience method to remove whitespace

@end

Building XMLParser.m

Lets start with our basic shell:

#import "Constants.h"
#import "SaleItem.h"
#import "XMLParser.h"

@implementation XMLParser
	// Synthesize our properties
	@synthesize currentValue;
	@synthesize itemElementInProgress;
	@synthesize saleItem;

	// Make sure objects are released
	-(void)dealloc{
		[currentValue release];
		[saleItem release];
		[super dealloc];
	}

@end

Now, let's create the initializer. Add the following code directly after the property @synthesize calls:

[cc lang="objc" tab_size="2" lines="-1"] // Sets the saleItem property to a SaleItem object passed in - (id)initWithSaleItem:(SaleItem *)aSaleItem{ if (self = [super init]) { [self setSaleItem:aSaleItem]; [self setItemElementInProgress:NO]; } return self; } [/cc]

I am initializing the class this way so I can populate the SaleItem inside of XMLParser and still have access to it from the calling class (a ViewController) when I'm done parsing.

Before we get into how to instantiate NSXMLParser and use it to parse a file, let's set up the delegate methods to get the data we need out of the file.

I usually put delegate methods at the bottom of the file, right after a #pragma mark definition to delineate this group of methods from the rest of the code. Hop down to the bottom of XMLParser.m, and place this code just prior to @end:

#pragma mark NSXMLParser Delegate Calls
// This method gets called every time NSXMLParser encounters a new element
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
	 namespaceURI:(NSString *)namespaceURI 
	qualifiedName:(NSString *)qualifiedName 
	   attributes:(NSDictionary *)attributeDict{
		// If element is named "item", set bool to true so we know
		// we are inside the element in other methods. This is needed
		// because "item" contains most of the data we need
		if ([elementName isEqualToString:ITEM]) {
			[self setItemElementInProgress:YES];
		}
	}

// This method gets called for every character NSXMLParser finds.
	-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
		// If currentValue doesn't exist, initialize and allocate
		if (!currentValue) {
			currentValue = [[NSMutableString alloc] init];
		}
		
		// Append the current character value to the running string
		// that is being parsed
		[currentValue appendString:string];
	}

The final delegate method is fired when NSXMLParser reaches the end of an element. Here is the place we can be sure we have gotten the full value of the element, and can now persist that data to our model object for use. right after the foundCharacters method, add this method:

// This method is called whenever NSXMLParser reaches the end of an element
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
									 namespaceURI:(NSString *)namespaceURI 
									qualifiedName:(NSString *)qName{
	[self trimString];  // Remove whitespace with a convenience method
	
	if ([elementName isEqualToString:ITEM]) {
		[self setItemElementInProgress:NO];  // If we are currently on the "item" element, then do not save value
	}


	// If we are processing an element inside of "item", 
	// then determine if we need to save the value	
	if (self.itemElementInProgress) {
		// Now check the element id against the ones we are 
		// interested in (using the Constants class values).
		// If we find one, set the corresponding property on the 
		// SaleItem object
		if ([elementName isEqualToString:TITLE]) {
			[[self saleItem] setItemTitle:currentValue];
		}
			 
		if ([elementName isEqualToString:PRICE]) {
			[[self saleItem] setItemPrice:currentValue];
		}
		
		if ([elementName isEqualToString:THUMBNAIL_IMAGE]) {
			[[self saleItem] setItemThumbnaillUrl:currentValue];
		}
		
		if ([elementName isEqualToString:SUBSTANDARD_IMAGE]) {
			[[self saleItem] setItemSubstandardImageUrl:currentValue];
		}
		
		if ([elementName isEqualToString:STANDARD_IMAGE]) {
			[[self saleItem] setItemStandardImageUrl:currentValue];
		}
		
		if ([elementName isEqualToString:DETAIL_IMAGE]) {
			[[self saleItem] setItemDetailImageUrl:currentValue];
		}

		if ([elementName isEqualToString:DESCRIPTION]) {
			[[self saleItem] setItemDescription:currentValue];
		}
		
		if ([elementName isEqualToString:SHIPPING_PRICE]) {
			[[self saleItem] setItemShippingCost:currentValue];
		}
		
		if ([elementName isEqualToString:SALE_URL]) {
			[[self saleItem] setItemSaleUrl:currentValue];
		}
	}
	
	[currentValue release];
	currentValue = nil;
}

The Parse Method

Finally, the whole process is started by the parse method:
		- (BOOL)parse{
		// Create and initialize an NSURL with the RSS feed address and use it to instantiate NSXMLParser
		NSURL *url = [[NSURL alloc] initWithString:@"http://www.woot.com/salerss.aspx"];
		NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];

		// Tell NSXMLParser that this class is its delegate
		[parser setDelegate:self];
		
		// Kick off file parsing
		[parser parse];
		
		// Clean up
		[url release];
		
		[parser setDelegate:nil];
		[parser release];
		return YES;
	}

Final Notes

Some final thoughts on the methodology I am using:

  • I want to get rid of the if statements and use Key-Value coding to set property values. I haven't gotten the chance to refactor this
  • I am very new to Cocoa, and any suggestions/comments are welcome and encouraged!