Latest 0.0.3
Homepage https://github.com/andreacipriani/NSObject-AGCDescription
License MIT
Platforms ios 7.0
Authors

CI Status
Version
License
Platform

Introduction

- (NSString*)description
{
   😱 😱 😱 ⌛️⌛️⌛️
}

It’s boring to write description methods. It’s even more boring to keep them updated everytime you modify a class. But great descriptions can improve the quality of your debugging… and save you time!

🎉 Don’t worry! It’s time to let AGCDescriptiondo the dirty work for you! 🎉

AGCDescription is a category on NSObject which uses the Objective-c runtime potential to automatically build the description string of any of your classes. And if you gonna change the class, the description will be automatically updated.

Example

Given a simple class representing a User:


@interface AGCUser : NSObject

- (instancetype)initWithUserId:(NSNumber*)userId username:(NSString*)username password:(NSString*)password userImage:(UIImage*)userImage;

@property (nonatomic, strong) NSNumber* userId;
@property (nonatomic, copy) NSString* username;
@property (nonatomic, copy) NSString* password;
@property (nonatomic, strong) UIImage* userImage;

Go the the implementation file, import the category:

#import "NSObject+AGCDescription.h"

And delegate AGCDescription to returns the description strings for you:

- (NSString*)description
{
  return [self agc_description];
}

- (NSString*)debugDescription
{
  return [self agc_debugDescription];
}

You’re done. It’s easy, no? Let’s now print the description of a user to see the result:

AGCUser* agcUser = [[AGCUser alloc] initWithUserId:@(123) username:@"Mick Jagger" password:@"angie123" userImage:[UIImage imageNamed:@"mick.png"]];
NSLog(@"%@",agcUser);

This is what will be logged:

<AGCUser:
{
userId = 123;
username = Mick Jagger;
password = angie123;
userImage = <UIImage: 0x7faa7a5fb170>, {140, 154};
}>

😎😎😎

Example details

And that’s not all! Suppose that after few days you decide that the user should also have an email address property.
You add it into your Class:

@property (nonatomic, copy) NSString* emailAddress;
//...
[agcUser setEmailAddress:@"[email protected]]

And you don’t need to change your code! The description will be automatically updated – the description is automatically updated also if you remove or rename a property.

<AGCUser:
{
userId = 123;
username = Mick Jagger;
password = angie123;
userImage = <UIImage: 0x7faa7a5fb170>, {140, 154};
emailAddress = [email protected]
}>

And what if you don’t want to log the password of the user? Or you just want to ignore a property (for example the userImage) that is not influent in the description? Use agc_descriptionIgnoringPropertiesWithNames method:

- (NSString*)description
{
  return [self agc_descriptionIgnoringPropertiesWithNames:@[@"password",@"userImage"]];
}

And you will protect your secrets 😉

<AGCUser:
{
userId = 123;
username = Mick Jagger;
emailAddress = [email protected]
}>

AGCDescription Rules

Let’s see how AGCDescription build the description string:

The format is:

<$ClassName:
{
$firstPropertyName = $firstPropertyValue;
...
$lastPropertyName = $lastPropertyValue;
}>    
  • Properties are in the some order of your code declaration
  • By default these properties are ignored: hash, description, debugDescription

Sometimes property values are too long to be into the description. In that cases a so called short description is used, which basically has this format:

<$ClassName: $objectAddress>

Here are the rules to evaluate the property value:

  • If a property is a primitive or a Foundation object with a known short description, print its description as the value
  • If a property is kind of a custom Class, print the short description
  • If the property is a collection, print all objects inside the collection
    • If one of this object is a custom class, print the short description
    • If one of this object is another collection, print the short description of the collection

Usage

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

iOS 8+ and ARC

Installation

AGCDescription is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "AGCDescription"

Installation without Cocoapods

Copy and paste all AGCDescription/Classes into your project.

Author

Andrea Cipriani, [email protected] – Twitter @AndreaCipriani

License

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

Latest podspec

{
    "name": "NSObject-AGCDescription",
    "version": "0.0.3",
    "summary": "It generates the description strings of your classes automatically.",
    "description": "It's boring to write description methods. It's even more boring to keep them updated everytime you modify a class. But great descriptions can improve the quality of your debugging... and save you time! Don't worry! It's time to let AGCDescriptiondo the dirty work for you! AGCDescription is a category on NSObject which uses the Objective-c runtime potential to automatically build the description string of any of your classes. And if you gonna change the class, the description will be automatically updated.",
    "homepage": "https://github.com/andreacipriani/NSObject-AGCDescription",
    "license": "MIT",
    "authors": {
        "Andrea Cipriani": "[email protected]"
    },
    "source": {
        "git": "https://github.com/andreacipriani/NSObject-AGCDescription.git",
        "tag": "0.0.3"
    },
    "social_media_url": "https://twitter.com/AndreaCipriani",
    "platforms": {
        "ios": "7.0"
    },
    "source_files": "NSObject-AGCDescription/Classes/**/*"
}

Pin It on Pinterest

Share This