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([NSObjectclass],"description")));// T@"NSString",R,C
AmIBeingDebugged
Nolan O’Brien brings the AmIBeingDebugged
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>
staticBoolAmIBeingDebugged(void){intmib[4];structkinfo_procinfo;size_tsize=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.
lazyvarsomeModelStructure=ExpensiveClass()
If you call
set
on 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*)seguesender:(id)sender{[superprepareForSegue:seguesender:sender];// 2. All known destination controllers assigned to propertiesif([selfrespondsToSelector:NSSelectorFromString(segue.identifier)]){[selfsetValue:segue.destinationViewControllerforKey:segue.identifier];}}-(void)viewDidLoad{[superviewDidLoad];// 3. Controllers already available bc viewDidLoad is called after prepareForSegueself.childController1.view.backgroundColor=[UIColorredColor];self.childController2.view.backgroundColor=[UIColorblueColor];}
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:
funcpathToFileInSharedSubfolder(file:String)->String{returnXCPSharedDataDirectoryPath+"/"+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:
varjsonReadError:NSError?letjsonData=NSFileManager.defaultManager().contentsAtPath(pathToFileInSharedSubfolder("data.json"))!letjsonArray=NSJSONSerialization.JSONObjectWithData(jsonData,options:nil,error:&jsonReadError)as[AnyObject]
…or pulling out a local image:
letimageView=UIImageView()imageView.image=UIImage(contentsOfFile:pathToFileInSharedSubfolder("image.png"))
The rest of this year’s reader submissions come from Cédric Luthi, who (as in yearspast) 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_BINARY
Xcode setting for command-line apps. It’s much easier to use than the-sectcreate __TEXT __info_plist
linker 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://4722772
in 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 +preferredLanguages
. 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.Imagine that the preferred language order is
{English, French}
but your app is German only. Calling[[NSLocale preferredLanguages] firstObject]
will give you English when you want German.The proper way to get the actual language used by the app is to use
[[NSBundle mainBundle] preferredLocalizations]
.
From the documentation:
An array of
NSString
objects 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 +canonicalLanguageIdentifierFromString:
in order to ensure a canonical language identifier.
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
valueForKey:
, since very few classes override+accessInstanceVariablesDirectly
to disable ivar access through Key-Value Coding.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:
@interfaceMPMoviePlayerController:NSObject<MPMediaPlayback>{void*_internal;// 4 = 0x4BOOL_readyForDisplay;// 8 = 0x8}
Since
id internal = [moviePlayerController valueForKey:@"internal"]
doesn’t work, here is the hardcore way to access the internal ivar:
idinternal=*((constid*)(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
NSDateFormatter -setDateFormat:
withoutNSDateFormatter +dateFormatFromTemplate:options:locale:
, you’re probably doing it wrong.From the documentation:
+(NSString*)dateFormatFromTemplate:(NSString*)templateoptions:(NSUInteger)optslocale:(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=[[NSLocalealloc]initWithLocaleIdentifier:@"en_US"];NSLocale*gbLocale=[[NSLocalealloc]initWithLocaleIdentifier:@"en_GB"];NSString*dateFormat;NSString*dateComponents=@"yMMMMd";dateFormat=[NSDateFormatterdateFormatFromTemplate:dateComponentsoptions:0locale:usLocale];NSLog(@"Date format for %@: %@",[usLocaledisplayNameForKey:NSLocaleIdentifiervalue:[usLocalelocaleIdentifier]],dateFormat);dateFormat=[NSDateFormatterdateFormatFromTemplate:dateComponentsoptions:0locale:gbLocale];NSLog(@"Date format for %@: %@",[gbLocaledisplayNameForKey:NSLocaleIdentifiervalue:[gbLocalelocaleIdentifier]],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
UITransitionView +defaultDurationForTransition:
method, and set a breakpoint for that method:
(lldb) br set -n "+[UITransitionView defaultDurationForTransition:]"
Present a modal view controller, you will hit the breakpoint, type
finish
to execute the method:
(lldb) finish
At that point
defaultDurationForTransition:
has executed, and you can read the result (it’s in thexmm0
register):
(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_ASSIGN
policy 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:
@interfaceWeakObjectContainter:NSObject@property(nonatomic,readonly,weak)idobject;@end@implementationWeakObjectContainter-(instancetype)initWithObject:(id)object{self=[superinit];if(!self){returnnil;}self.object=object;returnself;}@end
Then, associate the
WeakObjectContainter
withOBJC_ASSOCIATION_RETAIN(_NONATOMIC):
objc_setAssociatedObject(self,&MyKey,[[WeakObjectContainteralloc]initWithObject:object],OBJC_ASSOCIATION_RETAIN_NONATOMIC);
Use the
object
property to access it in order to get a zeroing weak reference to the desired object:
idobject=[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.