Reader Submissions - New Year's 2015
As we take a moment to reflect on our experiences over the past year, one thing is clear: 2014 was an incredible year professionally for Apple developers. So much has happened in such a short timespan, and yet it’s hard to remember our relationship to Objective-C before Swift, or what APIs could have captivated our imagination as much as iOS 8 or WatchKit.
It’s an NSHipster tradition to ask you, dear readers, to send in your favorite tips and tricks from the past year for publication over the New Year’s holiday. This year, with a deluge of new developments—both from Cupertino and the community at large—there were no shortage of interesting tidbits to share.
Thanks to Colin Rofls, Cédric Luthi, Florent Pillet, Heath Borders, Joe Zobkiw, Jon Friskics, Justin Miller, Marcin Matczuk, Mikael Konradsson, Nolan O’Brien, Robert Widmann, Sachin Palewar, Samuel Defago, Sebastian Wittenkamp, Vadim Shpakovski, and Zak Remer for their contributions.
The Secret Lives of Member Functions
From Robert Widmann:
Member functions on Swift classes and structures always have the following type when used statically:
Object -> (Args) -> Thing
For example, you can call reverse() on an array in two ways:
[1, 2, 3, 4].reverse()
Array.reverse([1, 2, 3, 4])()
@( ) for Boxing C-Strings
From Samuel Defago:
Given the fact that literals are most of the time associated with numbers and collections, I often forget that they work for UTF8-encoded
NULL-terminated C-strings as well, especially when I write code using the runtime:
NSString *propertyAttributesString =
@(property_getAttributes(class_getProperty([NSObject class], "description")));
// T@"NSString",R,C
AmIBeingDebugged
Nolan O’Brien brings the Am function to our attention from from this Technical Q&A document:
#include <assert.h>
#include <stdbool.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/sysctl.h>
static Bool AmIBeingDebugged(void) {
int mib[4];
struct kinfo_proc info;
size_t size = sizeof(info);
info.kp_proc.p_flag = 0;
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PID;
mib[3] = getpid();
sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0);
return (info.kp_proc.p_flag & P_TRACED) != 0;
}
Use Lazy Variables
From Colin Rofls:
Optionals should be avoided. Implicitly unwrapped optionals should be strongly avoided. Want to declare a var but don’t necessarily have an initial value at init time? Use the lazy keyword, and just don’t call the getter before you have your real value.
lazy var someModelStructure = ExpensiveClass()
If you call
seton this var without having ever called the getter, the lazy expression is never evaluated. Great for references to views that you don’t necessarily want to init until viewDidLoad, for instance.
Accessing Child Controllers Inserted Into Storyboard Container Views
From Vadim Shpakovski:
Here is a convenient way to access child controllers inserted into Storyboard container views:
// 1. A property has the same name as a segue identifier in XIB
@property (nonatomic) ChildViewController1 *childController1;
@property (nonatomic) ChildViewController2 *childController2;
// #pragma mark - UIViewController
- (void)prepareForSegue:(UIStoryboardSegue *)segue
sender:(id)sender
{
[super prepareForSegue:segue sender:sender];
// 2. All known destination controllers assigned to properties
if ([self respondsToSelector:NSSelectorFromString(segue.identifier)]) {
[self setValue:segue.destinationViewController forKey:segue.identifier];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// 3. Controllers already available bc viewDidLoad is called after prepareForSegue
self.childController1.view.backgroundColor = [UIColor redColor];
self.childController2.view.backgroundColor = [UIColor blueColor];
}
Re-Run without Re-Building
From Heath Borders:
If you’re repeatedly debugging the same problem over and over, you can run your app without rebuilding with “Product > Perform Action > Run without Building” (
⌘⌃R).
Quick Access to Playground Resources
From Jon Friskics:
Swift Playgrounds all share the same Shared Playground Data folder that’s symlinked to
/Users/HOME/Documents/Shared Playground Data.
If you like using lots of Playgrounds, you’ll want to organize the data that each Playground is using into subfolders of that shared folder, but then you’ve got to let the Playground know where to look. Here’s a helper function that I use that makes that easy:
func pathToFileInSharedSubfolder(file: String) -> String {
return XCPSharedDataDirectoryPath + "/" + NSProcessInfo.processInfo().processName + "/" + file
}
That processName property in NSProcessInfo contains the name of the Playground file, so as long as you have already created a sub-folder in the Shared Playground Data folder with the same name you can access those files pretty easily, like reading local JSON:
var jsonReadError:NSError?
let jsonData = NSFileManager.defaultManager().contentsAtPath(pathToFileInSharedSubfolder("data.json"))!
let jsonArray = NSJSONSerialization.JSONObjectWithData(jsonData, options: nil, error: &jsonReadError) as [AnyObject]
…or pulling out a local image:
let imageView = UIImageView()
imageView.image = UIImage(contentsOfFile: pathToFileInSharedSubfolder("image.png"))
The rest of this year’s reader submissions come from Cédric Luthi, who (as in years past) contributed a payload of tips and tricks worthy of an entire article unto themselves. Thanks so much for these, Cédric!
CocoaPods, Exposed!
Here’s a quick way to check all the pods used by a (closed source) app:
$ class-dump -C Pods_ /Applications/Squire.app | grep -o "Pods_\w+"
CREATE_INFOPLIST_SECTION_IN_BINARY
Check out the
CREATE_INFOPLIST_SECTION_IN_BINARYXcode setting for command-line apps. It’s much easier to use than the-sectcreate __TEXT __info_plistlinker flag and it embeds the processed Info.plist file into the binary.
It’s also a lesson on filing radars. This feature request was filed as
rdar://4722772in 2006 and was addressed about 7 years later.
Stop dylib Hooking
Make hackers’ lives tougher with this trick from Sam Marshall:
Add this one line to your “Other Linker Flags”:
-Wl,-sectcreate,__RESTRICT,__restrict,/dev/null
NSBundle -preferredLocalizations
Sometimes, you need to know which language your app is running in. Often, people will use
NSLocale +preferred. Unfortunately this tells nothing about the language the app is actually displaying. It will just give you the ordered list as found in “Settings → General → Language & Region → Preferred Language” Order on iOS, or “System Preferences → Language & Region → Preferred Languages” on OS X.Languages Imagine that the preferred language order is
{English, French}but your app is German only. Calling[[NSLocale preferredwill give you English when you want German.Languages] first Object] The proper way to get the actual language used by the app is to use
[[NSBundle main.Bundle] preferred Localizations]
From the documentation:
An array of
NSStringobjects containing language IDs for localizations in the bundle. The strings are ordered according to the user’s language preferences and available localizations.
From a comment in NSBundle.h:
A subset of this bundle’s localizations, re-ordered into the preferred order for this process’s current execution environment; the main bundle’s preferred localizations indicate the language (of text) the user is most likely seeing in the UI
You may also need to use
NSLocale +canonicalin order to ensure a canonical language identifier.Language Identifier From String:
Preserve SDK Headers
If you are installing Xcode from the dmg, check out this technique by Joar Wingfors in order to avoid to accidentally modifying SDK headers by preserving ownership, permissions and hard links:
$ sudo ditto /Volumes/Xcode/Xcode.app /Applications/Xcode.app
Inspecting void * Instance Variables
For reverse engineering purpose, it’s often useful to look at instance variables of objects. It’s usually pretty easy to do so with
value, since very few classes overrideFor Key: +accessto disable ivar access through Key-Value Coding.Instance Variables Directly There’s one case where it doesn’t work though: when the ivar has a
void *type.Here is an excerpt of the MediaPlayer framework class-dump on iOS 6.1:
@interface MPMoviePlayerController : NSObject <MPMediaPlayback>
{
void *_internal; // 4 = 0x4
BOOL _readyForDisplay; // 8 = 0x8
}
Since
id internal = [moviedoesn’t work, here is the hardcore way to access the internal ivar:Player Controller value For Key:@"internal"]
id internal = *((const id*)(void*)((uintptr_t)moviePlayerController + sizeof(Class)));
Don’t ship this code, its’s very fragile because the ivar layout may change. Use this for reverse engineering only!
NSDateFormatter +dateFormatFromTemplate:options:locale:
A friendly reminder that if you are using
NSDatewithoutFormatter -set Date Format: NSDate, you’re probably doing it wrong.Formatter +date Format From Template:options:locale: From the documentation:
+ (NSString *)dateFormatFromTemplate:(NSString *)template
options:(NSUInteger)opts
locale:(NSLocale *)locale
Different locales have different conventions for the ordering of date components. You use this method to get an appropriate format string for a given set of components for a specified locale (typically you use the current locale—see currentLocale).
The following example shows the difference between the date formats for British and American English:
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
NSString *dateFormat;
NSString *dateComponents = @"yMMMMd";
dateFormat = [NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:usLocale];
NSLog(@"Date format for %@: %@",
[usLocale displayNameForKey:NSLocaleIdentifier value:[usLocale localeIdentifier]], dateFormat);
dateFormat = [NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:gbLocale];
NSLog(@"Date format for %@: %@",
[gbLocale displayNameForKey:NSLocaleIdentifier value:[gbLocale localeIdentifier]], dateFormat);
// Output:
// Date format for English (United States): MMMM d, y
// Date format for English (United Kingdom): d MMMM y
Deriving Internal Constants with the Debugger
Recently, Matthias Tretter asked on Twitter:
Does anyone know the default animation duration and springs for modal viewController presentation on iOS 8?
— Matthias Tretter (@myell0w) November 21, 2014
Search for duration in a class-dump of UIKit, find the
UITransitionmethod, and set a breakpoint for that method:View +default Duration For Transition:
(lldb) br set -n "+[UITransitionView defaultDurationForTransition:]"
Present a modal view controller, you will hit the breakpoint, type
finishto execute the method:
(lldb) finish
At that point
defaulthas executed, and you can read the result (it’s in theDuration For Transition: xmm0register):
(lldb) register read xmm0 --format float64
xmm0 = {0.4 0}
Answer: the default duration is 0.4s.
DIY Weak Associated Objects
Unfortunately, the associated objects
OBJC_ASSOCIATION_ASSIGNpolicy does not support zeroing weak references. Fortunately, it’s quite easy to implement yourself. You just need a simple class to wrap an object with a weak reference:
@interface WeakObjectContainter : NSObject
@property (nonatomic, readonly, weak) id object;
@end
@implementation WeakObjectContainter
- (instancetype)initWithObject:(id)object {
self = [super init];
if (!self) {
return nil;
}
self.object = object;
return self;
}
@end
Then, associate the
WeakwithObject Containter OBJC_ASSOCIATION_RETAIN(_NONATOMIC):
objc_setAssociatedObject(self, &MyKey, [[WeakObjectContainter alloc] initWithObject:object], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
Use the
objectproperty to access it in order to get a zeroing weak reference to the desired object:
id object = [objc_getAssociatedObject(self, &MyKey) object];
And with that, we bring in a brand new year of possibilities and opportunities. Happy 2015, everyone!
May your code continue to compile and inspire.