Code Monkey home page Code Monkey logo

ctencodableobject's Introduction

CTEncodableObject

Base class which implements NSCoding, NSCopying for encoding, debugging, and quickLook.

This class basically falls down to one method, which returns a set of property names for that class which are readwrite and have storage

+ (NSMutableSet *)encodableKeys;

Because each class' properties are it's own, we must first call this method on it's superclass, and so-on, all the way up to NSObject.

    NSMutableSet *temp = [NSMutableSet set];
    Class superClass = class_getSuperclass(self);
    Method method = class_getClassMethod(superClass, _cmd);
    if(method != NULL)
        [temp unionSet:[superClass encodableKeys]]; //recursively get superclass properties

This is basically the crux of this class. We iterate through each of our properties using the objc-runtime method class_copyPropertyList(). We then inspect the attributes of each property, ensuring they are not weak and have storage (!readonly && !weak). If the property meets this criterea, we add it to the set of properties and move on to the next one.

    unsigned int count = 0;
    objc_property_t *properties = class_copyPropertyList(self, &count);
    for(int i = 0; i < count; i++)
    {
        objc_property_t prop = properties[i];
        char *readonly = property_copyAttributeValue(prop, ct_PropertyAttributeValueReadonly);
        char *weak = property_copyAttributeValue(prop, ct_PropertyAttributeValueWeak);
        if(!readonly && !weak)
            [temp addObject:@(property_getName(prop))];
        free(readonly);
        free(weak);
    }
    free(properties);

Lastly, remove any explicitly declared keys which should not be included, free up the memory, and return!

    [temp minusSet:[self unencodableKeys]];
    
    return temp;

License

CTEncodableObject is available under the MIT license. See the LICENSE file for more info.

ctencodableobject's People

Contributors

charlieelliott avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.