Created
June 3, 2014 21:48
-
-
Save wjlafrance/04fd2165a71b283e4075 to your computer and use it in GitHub Desktop.
Swift example - Debug Output
This file contains hidden or 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
#import <Foundation/Foundation.h> | |
@interface NSData (DebugOutput) | |
- (NSString *)debugOutputObjc; | |
- (NSString *)debugOutputSwift; | |
@end |
This file contains hidden or 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
#import "BridgeHeader.h" | |
@implementation NSData (DebugOutput) | |
- (NSString *)debugOutputObjc | |
{ | |
int rowlength = 16; | |
const uint8_t *data = [self bytes]; | |
NSMutableString *str = [NSMutableString new]; | |
for (NSInteger i = 0; i < (NSInteger) [self length]; ) { | |
[str appendFormat:@"\n%08x: ", i]; | |
for (NSInteger j = 0; j < rowlength; j++, i++) { | |
if (i >= (NSInteger) [self length] || i < 0) { | |
[str appendString:@" "]; | |
} else { | |
[str appendFormat:@"%02x ", data[i]]; | |
} | |
} | |
i -= rowlength; | |
[str appendString:@" "]; | |
for (NSInteger j = 0; j < rowlength; j++, i++) { | |
if (i >= (NSInteger) [self length] || i < 0) { | |
[str appendString:@" "]; | |
} else if (data[i] < 33 || data[i] > '~') { | |
[str appendString:@"."]; | |
} else { | |
[str appendFormat:@"%c", data[i]]; | |
} | |
} | |
} | |
return str; | |
} | |
@end |
This file contains hidden or 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
import Foundation | |
extension NSData { | |
func debugOutputSwift() -> NSString { | |
let rowLength = 16 | |
let str = NSMutableString() | |
let data = UnsafeArray(start: UnsafePointer<UInt8>(self.bytes), length: self.length) | |
for var rowOffset = 0; rowOffset < self.length; rowOffset += rowLength { | |
str.appendFormat("\n%08x: ", rowOffset) | |
for index in rowOffset .. rowOffset + rowLength { | |
if index >= self.length || index < 0 { | |
str.appendString(" ") | |
} else { | |
let byte = data[index] | |
str.appendFormat("%02x ", byte) | |
} | |
} | |
str.appendString(" ") | |
for index in rowOffset .. rowOffset + rowLength { | |
if index >= self.length || index < 0 { | |
str.appendString(" ") | |
} else { | |
let byte = data[index] | |
if byte < 33 || byte > 126 { | |
str.appendString(".") | |
} else { | |
str.appendFormat("%c", byte) | |
} | |
} | |
} | |
} | |
return str | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment