Created
July 31, 2019 21:54
-
-
Save AmitaiB/bfddf415a4342bea79afbe86ee9db373 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSObject+LogProperties.h | |
// JWPlayer-SDK-iOS-Demo | |
// | |
// Created by Amitai Blickstein on 6/15/19. | |
// Copyright © 2019 JWPlayer. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
NS_ASSUME_NONNULL_BEGIN | |
@interface NSObject (LogProperties) | |
- (NSString*)extractPrettyPropertiesString NS_SWIFT_NAME(prettyProperties()); | |
- (NSString*)extractAutomationFriendlyPropertiesString NS_SWIFT_NAME(automationFriendlyProperties()); | |
- (void)logProperties; | |
@end | |
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSObject+LogProperties.m | |
// JWPlayer-SDK-iOS-Demo | |
// | |
// Created by Amitai Blickstein on 6/15/19. | |
// Copyright © 2019 JWPlayer. All rights reserved. | |
// | |
#import "NSObject+LogProperties.h" | |
#import <objc/runtime.h> | |
@implementation NSObject (logProperties) | |
+ (NSString*)delimiter { | |
return @"^_"; // ASCII 31, control character, the "Unit Separator" | |
} | |
- (NSString*)extractAutomationFriendlyPropertiesString { | |
return [self extractPropertiesStringPrettified:NO]; | |
} | |
- (NSString*)extractPrettyPropertiesString { | |
return [self extractPropertiesStringPrettified:YES]; | |
} | |
- (NSString*)extractPropertiesStringPrettified:(BOOL)prettified { | |
NSMutableString *result = [NSMutableString stringWithString:@""]; | |
@autoreleasepool { | |
unsigned int numberOfProperties = 0; | |
objc_property_t *propertyArray = class_copyPropertyList([self class], &numberOfProperties); | |
for (NSUInteger i = 0; i < numberOfProperties; i++) { | |
objc_property_t property = propertyArray[i]; | |
NSString *name = [NSString stringWithUTF8String:property_getName(property)]; | |
if ([self valueForKey:name]) { | |
[result appendFormat:@"%@:%@\n", name, [self valueForKey:name]]; | |
} else { | |
[result appendFormat:@"%@:<nil>\n", name]; | |
} | |
} | |
if (!prettified) { | |
// replace newlines with delimiter | |
[result replaceOccurrencesOfString:@"[\r\n]" | |
withString:[NSObject delimiter] | |
options:NSRegularExpressionSearch | |
range:NSMakeRange(0, result.length)]; | |
} | |
free(propertyArray); | |
} | |
if (result.length == 0) { | |
return @" ** No Properties **"; | |
} | |
return result.copy; | |
} | |
- (void)logProperties { | |
NSLog(@"\n----------------------------------------------- Properties for object %@", self); | |
NSLog(@"%@", [self extractPrettyPropertiesString]); | |
NSLog(@"\n-----------------------------------------------"); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment