TestsTested | ✓ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Jul 2016 |
Maintained by Andrea Cipriani.
- (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!
AGCDescription
do 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.
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};
}>
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]
}>
Let's see how AGCDescription
build the description string:
The format is:
<$ClassName:
{
$firstPropertyName = $firstPropertyValue;
...
$lastPropertyName = $lastPropertyValue;
}>
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:
To run the example project, clone the repo, and run pod install
from the Example directory first.
iOS 8+ and ARC
AGCDescription is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "AGCDescription"
Andrea Cipriani, [email protected] - Twitter @AndreaCipriani
AGCDescription is available under the MIT license. See the LICENSE file for more info.