NSLog - How to print object name?
Use the following code:
#define stringVariable(x) NSLog( @"%s:%@",#x, x)
NSString *myString=@"Welcome";
stringVariable(myString);
Note: The general principle is that when you put a # in front of an argument within the body of a #define, the preprocessor replaces it with a C string of the exact expression passed to the macro. When you pass a variable name, you'll get that name.
print() to console log with color
Nowadays, Xcode debugging console doesn't support coloring.
NSLog symbol print on console?]
try this
NSLog(@"str : %%");
Thanks
Xcode: How to print colored PRINT statement in console output?
That feature has been killed by Apple from Xcode 8.
But you can use SwiftyBeaver to achieve it.
let log = SwiftyBeaver.self
let console = ConsoleDestination()
log.addDestination(console)
// default xcode colors
console.levelColor.verbose = " "
How to convert UIColor to HEX and display in NSLog
Swift 5:
func hexStringFromColor(color: UIColor) -> String {
let components = color.cgColor.components
let r: CGFloat = components?[0] ?? 0.0
let g: CGFloat = components?[1] ?? 0.0
let b: CGFloat = components?[2] ?? 0.0
let hexString = String.init(format: "#%02lX%02lX%02lX", lroundf(Float(r * 255)), lroundf(Float(g * 255)), lroundf(Float(b * 255)))
print(hexString)
return hexString
}
func colorWithHexString(hexString: String) -> UIColor {
var colorString = hexString.trimmingCharacters(in: .whitespacesAndNewlines)
colorString = colorString.replacingOccurrences(of: "#", with: "").uppercased()
print(colorString)
let alpha: CGFloat = 1.0
let red: CGFloat = self.colorComponentFrom(colorString: colorString, start: 0, length: 2)
let green: CGFloat = self.colorComponentFrom(colorString: colorString, start: 2, length: 2)
let blue: CGFloat = self.colorComponentFrom(colorString: colorString, start: 4, length: 2)
let color = UIColor(red: red, green: green, blue: blue, alpha: alpha)
return color
}
func colorComponentFrom(colorString: String, start: Int, length: Int) -> CGFloat {
let startIndex = colorString.index(colorString.startIndex, offsetBy: start)
let endIndex = colorString.index(startIndex, offsetBy: length)
let subString = colorString[startIndex..<endIndex]
let fullHexString = length == 2 ? subString : "\(subString)\(subString)"
var hexComponent: UInt32 = 0
guard Scanner(string: String(fullHexString)).scanHexInt32(&hexComponent) else {
return 0
}
let hexFloat: CGFloat = CGFloat(hexComponent)
let floatValue: CGFloat = CGFloat(hexFloat / 255.0)
print(floatValue)
return floatValue
}
How to use
let red = CGFloat(30.0)
let green = CGFloat(171.0)
let blue = CGFloat(13.0)
let alpha = CGFloat(1.0)
let color = UIColor(red: CGFloat(red/255.0), green: CGFloat(green/255.0), blue: CGFloat(blue / 255.0), alpha: alpha)
let colorCode = self.hexStringFromColor(color: color)
print(colorCode)
let resultColor = self.colorWithHexString(hexString: colorCode)
print(resultColor)
Objective-C:
- (NSString *)hexStringFromColor:(UIColor *)color {
const CGFloat *components = CGColorGetComponents(color.CGColor);
CGFloat r = components[0];
CGFloat g = components[1];
CGFloat b = components[2];
return [NSString stringWithFormat:@"#%02lX%02lX%02lX",
lroundf(r * 255),
lroundf(g * 255),
lroundf(b * 255)];
}
After getting hex code string, Call below method to get UIColor
- (UIColor *) colorWithHexString: (NSString *) hexString
{
NSString *colorString = [[hexString stringByReplacingOccurrencesOfString: @"#" withString: @""] uppercaseString];
NSLog(@"colorString :%@",colorString);
CGFloat alpha, red, blue, green;
// #RGB
alpha = 1.0f;
red = [self colorComponentFrom: colorString start: 0 length: 2];
green = [self colorComponentFrom: colorString start: 2 length: 2];
blue = [self colorComponentFrom: colorString start: 4 length: 2];
return [UIColor colorWithRed: red green: green blue: blue alpha: alpha];
}
- (CGFloat) colorComponentFrom: (NSString *) string start: (NSUInteger) start length: (NSUInteger) length {
NSString *substring = [string substringWithRange: NSMakeRange(start, length)];
NSString *fullHex = length == 2 ? substring : [NSString stringWithFormat: @"%@%@", substring, substring];
unsigned hexComponent;
[[NSScanner scannerWithString: fullHex] scanHexInt: &hexComponent];
return hexComponent / 255.0;
}
How to use
// ( R = 30, G = 171, B = 13)?
CGFloat red = 30.0;
CGFloat green = 171.0;
CGFloat blue = 13.0;
CGFloat alpha = 255.0
UIColor *color = [UIColor colorWithRed:(red/255.0) green:(green/255.0) blue:(blue/255.0) alpha:(alpha/255.0)];
NSString *colorCode = [self hexStringFromColor:color];
NSLog(@"Color Code: %@", colorCode);
UIColor *resultColor = [self colorWithHexString:colorCode];
Is there any way to clear NSLog Output?
You can use a conditional breakpoint to simulate it. Define a function like this in your code:
int clear_console()
{
NSLog(@"\n\n\n\n\n\n\n\n");
}
Then, when you want to clear the console just add a breakpoint before the NSLog with this condition:
- Condition: 1 > 0
- Action: Debugger Command
expr (int) clear_console()
- Options: Automatically continue after evaluating Check it to skip the pause.
Tested with Xcode 4.3.2 and lldb.
Previous answer:
AFAIK, no, there isn't.
Just in case you're not doing it yet, you can create custom macros to format the output to highlight what you want.
Define macros like this:
#define CLEAR(...) NSLog(@"\n\n\n\n\n\n") /* enough \n to "clear" the console */
#define WTF(...) CLEAR();NSLog(@"!!!!!!!!!!!!!!");NSLog(__VA_ARGS__)
#define TRACE(__message__) NSLog(@">>>>>>>>>>>>>>> %@ <<<<<<<<<<<<<<<<<<<", __message__)
Then:
WTF(@"This should't be here object: %@", theObject);
...
TRACE(@"Start Encoding");
...
It's not what you want but it pretty much solves the problem. You'll end up with your own set of macros with custom prefixes easily scannable in the console output.
Related Topics
How to Link Googleanalytics 3.01 with Xcode 5 (Missing Required Architecture X86_64)
Could Not Instantiate Class Named Ibnslayoutconstraint
iOS - Return to Safari from Native App Without Opening New Tab
Ios: Can't Save File to 'Application Support' Folder, But Can to 'Documents'
How to Set Fake Gps Location on iOS Real Device
Nsdictionaryofvariablebindings Swift Equivalent
Uitableviewcell Separator Disappearing in iOS7
How to Rearrange Views When Autorotating with Autolayout
Memory-Mapped Files and Low-Memory Scenarios
Can't Set Titleview in the Center of Navigation Bar Because Back Button
Number of Threads Created by Gcd
Uitextfield Only Top and Bottom Border
Xcode 8: Preparing Archive Takes Forever
How to Show Complete List When Keyboard Is Showing Up in Swiftui