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 *property Attributes String =
@(property_get Attributes(class_get Property([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 Am IBeing Debugged(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 some Model Structure = Expensive Class()
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) Child View Controller1 *child Controller1;
@property (nonatomic) Child View Controller2 *child Controller2;
// #pragma mark - UIView Controller
- (void)prepare For Segue:(UIStoryboard Segue *)segue
sender:(id)sender
{
[super prepare For Segue:segue sender:sender];
// 2. All known destination controllers assigned to properties
if ([self responds To Selector:NSSelector From String(segue.identifier)]) {
[self set Value:segue.destination View Controller for Key:segue.identifier];
}
}
- (void)view Did Load {
[super view Did Load];
// 3. Controllers already available bc view Did Load is called after prepare For Segue
self.child Controller1.view.background Color = [UIColor red Color];
self.child Controller2.view.background Color = [UIColor blue Color];
}
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 path To File In Shared Subfolder(file: String) -> String {
return XCPShared Data Directory Path + "/" + NSProcess Info.process Info().process Name + "/" + 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 json Read Error:NSError?
let json Data = NSFile Manager.default Manager().contents At Path(path To File In Shared Subfolder("data.json"))!
let json Array = NSJSONSerialization.JSONObject With Data(json Data, options: nil, error: &json Read Error) as [Any Object]
…or pulling out a local image:
let image View = UIImage View()
image View.image = UIImage(contents Of File: path To File In Shared Subfolder("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_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 +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 preferred
will 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
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 +canonical
in 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
void *
Instance Variables
Inspecting 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: +access
to 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 MPMovie Player Controller : NSObject <MPMedia Playback>
{
void *_internal; // 4 = 0x4
BOOL _ready For Display; // 8 = 0x8
}
Since
id internal = [movie
doesn’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)movie Player Controller + sizeof(Class)));
Don’t ship this code, its’s very fragile because the ivar layout may change. Use this for reverse engineering only!
NSDate Formatter +date Format From Template:options:locale:
A friendly reminder that if you are using
NSDate
withoutFormatter -set Date Format: NSDate
, you’re probably doing it wrong.Formatter +date Format From Template:options:locale: From the documentation:
+ (NSString *)date Format From Template:(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 *us Locale = [[NSLocale alloc] init With Locale Identifier:@"en_US"];
NSLocale *gb Locale = [[NSLocale alloc] init With Locale Identifier:@"en_GB"];
NSString *date Format;
NSString *date Components = @"y MMMMd";
date Format = [NSDate Formatter date Format From Template:date Components options:0 locale:us Locale];
NSLog(@"Date format for %@: %@",
[us Locale display Name For Key:NSLocale Identifier value:[us Locale locale Identifier]], date Format);
date Format = [NSDate Formatter date Format From Template:date Components options:0 locale:gb Locale];
NSLog(@"Date format for %@: %@",
[gb Locale display Name For Key:NSLocale Identifier value:[gb Locale locale Identifier]], date Format);
// 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</blockquote>
Search for duration in a class-dump of UIKit, find the UITransition View +default Duration For Transition:
method, and set a breakpoint for that method:
(lldb) br set -n "+[UITransition View default Duration For Transition:]"
Present a modal view controller, you will hit the breakpoint, type finish
to execute the method:
(lldb) finish
At that point default Duration For Transition:
has executed, and you can read the result (it’s in the xmm0
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:
@interface Weak Object Containter : NSObject
@property (nonatomic, readonly, weak) id object;
@end
@implementation Weak Object Containter
- (instancetype)init With Object:(id)object {
self = [super init];
if (!self) {
return nil;
}
self.object = object;
return self;
}
@end
Then, associate the Weak Object Containter
with OBJC_ASSOCIATION_RETAIN(_NONATOMIC):
objc_set Associated Object(self, &My Key, [[Weak Object Containter alloc] init With Object:object], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
Use the object
property to access it in order to get a zeroing weak reference to the desired object:
id object = [objc_get Associated Object(self, &My Key) 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.