Tag Archives: Objective C

Autolayout: change constraint multiplier

the NSLayoutConstraint multiplier property is read-only. But if you need to change it, you can replace the constraint with a modified clone like @implementation NSLayoutConstraint(ChangeMultiplier) // visal form center http://stackoverflow.com/a/13148012/349514 -(NSLayoutConstraint *)constraintWithMultiplier:(CGFloat)multiplier { return [NSLayoutConstraint constraintWithItem:self.firstItem attribute:self.firstAttribute relatedBy:self.relation toItem:self.secondItem attribute:self.secondAttribute multiplier:multiplier constant:self.constant]; } @end@implementation NSLayoutConstraint(ChangeMultiplier) // visal form center http://stackoverflow.com/a/13148012/349514 -(NSLayoutConstraint *)constraintWithMultiplier:(CGFloat)multiplier { return [NSLayoutConstraint […]

Catch JavaScript exceptions in UIWebViews

A small but useful category method on UIWebView: Turn uncaught JavaScript Exception into a NSError! // // Created by Marcus Rohrmoser on 05.12.13. // Copyright (c) 2013 Marcus Rohrmoser mobile Software. All rights reserved. //   #import "UIWebView+JavaScriptNSError.h"   #define NSERROR_UIWEBVIEW_SCRIPT @"NSERROR_UIWEBVIEW_SCRIPT" #define NSERROR_UIWEBVIEW_SCRIPT_CODE 1   @implementation UIWebView(JavaScriptNSError)   -(NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script error:(NSError * __autoreleasing *)error […]

UIColor from hex string, once again

I end up doing this once per year – at least it’s getting leaner each time. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 […]

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 […]