Tag Archives: Objective C

UILabel with a (custom) CGFont

UILabel’s font property accepts UIFonts – but strange enough there’s no way to get a custom loaded CGFont (from a ttf or otf file) converted into such an UIFont. You’re stuck with the iPhone’s pre-installed fonts – at least when you have to support iOS 3.0 devices. After googling a bit and searching Stackoverflow I found [...]

XCode: missing “deprecated” warnings

when developing for long-term use, you want to use APIs that aren’t likely to be removed soon, a.k.a. “deprecated”. So, don’t use downward compatible calls below a point you really aim for. XCode helps with compiler warnings about “deprecated” calls – if “Project -> Edit Project Settings -> GCC_WARN_ABOUT_DEPRECATED_FUNCTIONS” is set: But be careful, this [...]

iPhone Unit Test Coverage

have a look at the CoverStory Howto, download CoverStory, open (with XCode) the XCode Project you want to measure, run the script EnableGCov.scpt linked to from the howto, run your tests and see a linker error, – dead end for the time being. http://groups.google.com/group/coverstory-discuss/browse_thread/thread/fbcbf5ed61d8d02b#

NSCachedURLResponse / NSKeyedUnarchiver pain

as the iPhone SDK comes with a rather dysfunctional NSURLCache — Apple suggests to implement it from scratch yourself in the code examples about caching — I went for just this. Until I came across the [NSKeyedUnarchiver unarchiveObjectWithData:...] not restoring userInfo, storagePolicy and data of NSCachedURLResponse. Couldn’t believe it and spent almost the whole day verifying [...]

Binary Search NSArray

Though CFArray comes with binary search capability, NSArray does not – at least not within the iPhone SDK. The indexOfObject:inSortedRange:options:usingComparator: can’t be found. Plus the CFArrayBSearchValues doesn’t tell you whether the key actually is part of the list or not. That’s what the Java JDK does, so let’s implement some category methods -(NSInteger)binarySearch:(id)key; -(NSInteger)binarySearch:(id)key usingSelector:(SEL)comparator; [...]

CoreData generic findManyByKey

The base for many of my SELECT-ish queries when querying by exact match is one generic method I created in some category methods on NSManagedObjectContext: -(NSArray*)entityName:(NSString*)entityName findManyByRelation:(NSDictionary*)dict { // TODO handle dict nil and emptyness NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:dict.count]; for(NSString *key in dict) { NSExpression *left = [NSExpression expressionForKeyPath:key]; NSExpression *right = [NSExpression [...]

NSDateFormatter case sensitive trap

Though NSDateFormatter behaves slightly different than documented, the following might even be correct, as strange as it might look (mind the last two lines): -(void)testNSDateFormatterTrap { NSDateFormatter *lower = [[[NSDateFormatter alloc] init] autorelease]; lower.dateFormat = @"yyyy-MM-dd HH:mm:SS ZZZ";   NSDateFormatter *upper = [[[NSDateFormatter alloc] init] autorelease]; upper.dateFormat = @"YYYY-MM-dd HH:mm:SS ZZZ";   lower.timeZone = upper.timeZone [...]

CocoaTouch, CoreData and binary String Search

The query optimiser for NSPredicate queries ontop CoreData/SQLite on the iPhone is a bit rudimentary (cough) and so I had to optimise myself to get binary-search enabled quick results:

Cocoa wrapped regex.h

Strange enough there’s no regular expression class in the iPhone SDK. Update: iOS 4 brings NSRegularExpression. My simple wrapper around the regex.h C API is not safe for unicode matching patterns but does the job e.g. for parsing URLs. If you need more, have a look at RegexKitLite. My simple wrapper has the interface:

NSDateFormatter & Http Header

Ever tried to get e.g. the “Last-Modified” HTTP response header field into a NSDate object? That’s no real fun, because this standard formatting isn’t digested by default, the required format string doesn’t quite work as documented.