Skip to content

Instantly share code, notes, and snippets.

@davbeck
Last active Aug 29, 2015
Embed
What would you like to do?
TNKPropertyKey

It's become common practice to use an @selector for associated objects. This is useful because not only are SELs guaranteed to have a unique address, but because as of Xcode 5, Xcode will warn you when you use a selector that it does not know about. Meaning that it is somewhat protected against spelling mistakes.

This can also be useful when using string keys. For things like state restoration, where you want a key for a given property, you can use the selector for the getter, and get a string from that. Xcode will autocomplete the name for you, and will warn you if you misspell it.

- (void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
    [super encodeRestorableStateWithCoder:coder];
    
    [coder encodeObject:self.feedID forKey:TNKPropertyKey(feedID)];
}
//
// Created by David Beck on 3/18/14.
// Copyright (c) 2014 ThinkUltimate. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#define TNKPropertyKey(property) NSStringFromSelector(@selector(property))
@davbeck
Copy link
Author

davbeck commented Apr 7, 2014

I was looking through the source for AFNetworking and found this:

- (id)initWithCoder:(NSCoder *)decoder {
    NSURL *baseURL = [decoder decodeObjectForKey:NSStringFromSelector(@selector(baseURL))];
    NSURLSessionConfiguration *configuration = [decoder decodeObjectForKey:@"sessionConfiguration"];
    if (!configuration) {
        NSString *configurationIdentifier = [decoder decodeObjectForKey:@"identifier"];
        if (configurationIdentifier) {
            configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:configurationIdentifier];
        }
    }

    self = [self initWithBaseURL:baseURL sessionConfiguration:configuration];
    if (!self) {
        return nil;
    }

    self.requestSerializer = [decoder decodeObjectForKey:NSStringFromSelector(@selector(requestSerializer))];
    self.responseSerializer = [decoder decodeObjectForKey:NSStringFromSelector(@selector(responseSerializer))];

    return self;
}

Looks like I wasn't the only one that had the idea to do this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment