March 1, 2010
Of all the JSON parsing libraries for the iPhone, the one I like the most is YAJL-obj, which is based on the “event driven/SAX style” YAJL. When I first started creating iPhone applications, I used a lot of Plists; I’ve now switched this all over to JSON not only because it’s more standards-y, but also because I can use the same data sets with my Python/Django and Android applications.
The JSON data I’m working with looks something like a dump of a SQL Table: lots of rows using the same column names — i.e. dictionary keys – over and over. If you’re loading a couple of thousand rows of data, each with 20 or or 30 columns/keys, the shear size of the key strings can add up — even though really there’s only a couple of dozen keys in total really in use. All these duplications waste a lot of memory – which is especially tight on a 8Gb iPod Touch – so I decided it might help out to actually reuse keys in my JSON loaders.
Here’s the changes I made to YAJL:
YAJLDocument.h
@interface YAJLDocument : NSObject {
...
NSMutableDictionary *keyStore_;
}
@property(nonatomic,retain) NSMutableDictionary *keyStore_;
YAJLDocument.m
- (id)initWithParserOptions:(YAJLParserOptions)parserOptions {
if ((self = [super init])) {
...
keyStore_ = [[NSMutableDictionary alloc] initWithCapacity:16];
}
return self;
}
- (void)parser:(YAJLParser *)parser didMapKey:(NSString *)key {
#if 1 // DPJ 2010-03-01
key_ = [keyStore_ objectForKey:key];
if (!key_) {
[keyStore_ setObject:key forKey:key];
key_ = key;
}
#else
key_ = key;
#endif
[keyStack_ addObject:key_]; // Push
}
To rebuild your library:
- select build target Device – 3.0 | Release | Combine Libs
- the output file is
Project-IPhone/build/libYAJLIPhone-*.zip
- just add all the things in that ZIP file to your iPhone project and you’re good to go
Final thoughts:
- whether this happens or not should be controlled by the YAJLParserOptions
- I’m probably going keep modifying this code, adding a version with a “scrubbing delegate” that allows dictionaries to be scrubbed on the spot of data not needed in the application
November 28, 2009
There may be some temptation to improve caching for UIImage imageNamed:
static UIImage* starred = nil;
if (starred == nil) starred = [UIImage imageNamed:@"starred.png"];
Don’t do this, it’s really stupid. It’s already being held by a cache, and maybe dropped out of it at any time, leading to miraculous crashes.
November 8, 2009
This was something I was hoping would be in the 3GS. Rumor: Next generation iPhone to be RFID enabled:
A highly reliable source has informed me that Apple has built some prototypes of the next gen iPhone with an RFID reader built in and they have seen it in action. So its not full NFC but its a start for real service discovery and I’m told that the reaction was very positive that we can expect this in the next gen iPhone.
If Apple does it, expect every phone manufacturer and their sister to begin pumping out NFC enabled phones, at least for service discovery and sync.
This just reinforces what we knew based on the two separate patents Apple submitted that had the iPhone enabled to read RFID tags. I’m told that the touch project video and the BT SIG’s specs were all driving forces to push this forward as well as other factors.
Guess I’ll be touching my iPhone to my Mac to link them together to sync iTunes by next year.
My thoughts:
- this will be a game changer for RFID, bringing applications down to the small business and hobbyist layer
- RFID + applications is a wicked combination, making the mobile phone an Anything Device; Microsoft, Motorola and RIM should all try to get the jump on this
- RFID + in app purchase is a wicked combination; put your thinking cap on for this one
- RFID + Augmented Reality makes context sensitive layers start popping up
- I’m not a game person, but you know there’s got to be gaming implications for this
- what are the implications for big chains — groceries, consumer electronics — when anyone can walk in the door and get a better price buy waving their phone at a shelf
September 26, 2009
This is an example of how to combine a UIPageControl and a UIScrollView together to create a “snap to page”-like effect that is seen on the iPhone’s home screen. This sample is partially based on Apple’s UIPageControl example.
XCode Sample Project
June 27, 2009
Alec Saunders has a seriously cool article on his blog about how your iPhone knows where it is. I got to experience how well this works yesterday when we used TimmyMe in the McCowan Road/Ellesmere Avenue neighborhood to grab a coffee pre-meeting:
Many folks assume that iPhone’s location services are GPS based. In fact, they’re not. [...]
Skyhook’s innovation is to augment that with WiFi hotspot locations. Why WiFi? It can be wonderfully accurate, to within 30 to 60 feet, or 10 to 20m which is roughly the same as GPS. WiFi can establish a fix within less than a second. And, WiFi is not vulnerable to overhead obstructions, so it can be used indoors.
Skyhook’s technology relies on a database of WiFi access points in over 2,000 cities (and growing). Much like Google, they use vehicles to drive cities, and using a laptop in the vehicle, they chart the location of WiFi access points. In addition, on iPhone as users use the mapping application, new locations and newly discovered WiFi access points are automatically added to the database. It’s a kind of automated crowdsourcing, based on usage.
My emphasis on that last sentence. Very cool.
April 22, 2009
We’ll be holding the next Coffee & iPhone tomorrow between 4 and 6p again at the Dark Horse Cafe, 215 Spadina Avenue for Coffee and iPhone.
- What: Coffee & iPhone
- When: Thursday, April 23 from 4 – 6p
- Where: Dark Horse Cafe, 215 Spadina Avenue (at Sullivan Street)

April 15, 2009
Join myself, Blake, Wayne (and more!) tomorrow between 4 and 6p at the Dark Horse Cafe, 215 Spadina Avenue (just north of Queen) for Coffee and iPhone. Bring your MacBook, your iPod, your iPod Touch or just yourself and we’ll talk all things iPhone and mobile.
- What: Coffee & iPhone
- When: Thursday, April 16 from 4 – 6p
- Where: Dark Horse Cafe, 215 Spadina Avenue (at Sullivan Street)

At the last Coffee & iPhone we had the following attendees:
Note that this is the day before Joey’s Coffee & Code, at the same location!
April 1, 2009
Join myself, Blake, Wayne and more tomorrow between 1 and 3pm at the Lettieri Cafe at 581 Bloor West (at the corner with Bathurst) for “Coffee and iPhone”. Bring your MacBook and we’ll talk all things iPhone development.
View Larger Map
March 27, 2009
This code could be very useful for iPhone developers:
The name of the new project is Three20, after the 320-pixel-wide screen of the iPhone. The code is all hosted on github for your cloning pleasure. There is an excellent sample app called TTCatalog which lets you play with all of the various UI components. Documentation? Well… there are instructions for how to add Three20 to your project, but I am still working on comprehensive documentation for each of the classes. For now, the sample app and the code itself are your documentation.
The projects are:
- Photo viewer
- Message composer
- Web image views
- Internet-aware table view
- Better text fields (including type-ahead)
- HTTP disk cache
- URL-based navigation (this could be interesting)
The source base is under the Apache license.
March 23, 2009
If you have not created a XIB, do the following:
- select Resources in the Interface Building
- press ⌘N
- select User Interfaces in iPhone OS
- select View XIB … and create in the normal way
To create the Simple Table in the Interface Builder
- open the XIB in Resources
- drag the Table View from the Library to the View … it should resize to the full extent of the View
- select the Table View object and press ⌘2
- from the Outlets section, drag
- dataSource to File’s Owner
- delegate to File’s Owner
- save
Make your Controller.h a data source and delegate for the table:
@interface ThemesController : UIViewController
<UITableViewDelegate, UITableViewDataSource>
{
}
Add the following basic code to the Controller.m file:
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger) tableView:(UITableView*) tableView numberOfRowsInSection:(NSInteger) section
{
return ... the number of rows ...;
}
- (UITableViewCell*) tableView:(UITableView*) tableView
cellForRowAtIndexPath:(NSIndexPath*) indexPath
{
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier: SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.text = ... an NSString containing what to display for row row ...;
// cell.image = [UIImage imageNamed:@"star.png"];
return cell;
}
#pragma mark -
#pragma mark Table Delegate Methods
- (void) tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
}
Compile and run.
Property Lists – plists – are a standard way of storing data in iPhone and MacOS programs. Here’s how you use them on the iPhone.
Add this code to Controller.m to populate member listData with the data from plist dwarves.plist:
- (void) viewDidLoad
{
NSBundle* bundle = [NSBundle mainBundle];
NSString* plistPath = [bundle pathForResource:@"dwarves" ofType:@"plist"];
NSArray* dwarves = [[NSArray alloc] initWithContentsOfFile:plistPath];
self.listData = dwarves;
[dwarves release];
[super viewDidLoad];
}
To create the plist:
- open and select the Resources folder in Interface Builder
- right-click and select Add > New File…
- select Other in the popup (at the bottom)
- select the Property List icon and then press the Next button
- choose your name and Finish
To edit the plist:
- select it in the Resources Folder
- right-click and select Open with Finder
Make sure:
- the name of the plist in the code (
dwarves) is the same as the plist you created
- the type of the plist in the code (
NSArray) is the same as the plist you created – by default it is actually NSDictionary, which is the other common option