Is there a Swift alternative for NSLog(@%s, __PRETTY_FUNCTION__)
Swift has #file
, #function
, #line
and #column
. From Swift Programming Language:
#file
- String - The name of the file in which it appears.
#line
- Int - The line number on which it appears.
#column
- Int - The column number in which it begins.
#function
- String - The name of the declaration in which it appears.
What is the replacement of __PRETTY_FUNCTION__ in Swift?
In Swift, use "\(#function)"
to replace __PRETTY_FUNCTION__
Using os_log to log function arguments, or other dynamic data
See Logging:
Formatting Log Messages
To format a log message, use a standard NSString or printf format string, ...
and String Format Specifiers for the standard format string specifiers, such as %@
and %d
.
In your case:
os_log("foo: %@ %@", log: .default, type: .debug, x, y.description)
The format string is restricted to static strings to prevent
(unintentional) expansion of format string specifiers. Here is an example demonstrating the
problem, using NSLog()
because that does not restrict the format
to constant strings:
let s = "50%"
NSLog("\(s)percent")
// Output: 500x0ercent
The %p
expects a pointer on the variable argument list, which is
not provided. This is undefined behavior, it can lead to crashes
or unexpected output.
NSLog on devices in iOS 10 / Xcode 8 seems to truncate? Why?
A temporary solution, just redefine all NSLOG
to printf
in a global header file.
#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
Related Topics
How to Create Instances of Managed Object Subclasses in a Nsmanagedobject Swift Extension
What Is the In-Practice Difference Between Generic and Protocol-Typed Function Parameters
Why Can't I Instantiate an Empty Array of a Nested Class
Swift Beta Performance: Sorting Arrays
Why Is Swift Compile Time So Slow
How to Create a Global Variable
Swift 5.0: 'Withunsafebytes' Is Deprecated: Use 'Withunsafebytes≪R≫(...)
Swift: Print() VS Println() VS Nslog()
Sort Dictionary by Values in Swift
The Use of Swift 3 @Objc Inference in Swift 4 Mode Is Deprecated
How to Test Equality of Swift Enums With Associated Values
How to Apply the Type to a Nsfetchrequest Instance
Swift Dictionary Get Key For Value
Why Is an Observedobject Array Not Updated in My Swiftui Application
Read and Write a String from Text File