Is there a way to get line number and function name in Swift language?
The Swift Language Reference defines a few "special literals" that offer this behavior:
Literal Type Value
#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.
How to print out the method name and line number in swift
Literal Type Value
#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.
#dsohandle UnsafeMutablePointer The dso handle.
Example
print("Function: \(#function), line: \(#line)")
With default values in parameters you can also create a function
public func track(_ message: String, file: String = #file, function: String = #function, line: Int = #line ) {
print("\(message) called from \(function) \(file):\(line)")
}
which can be used like this
track("enters app")
In Swift 2.1
Literal Type Value
__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.
for more info see the documentation
Log line number and method name in Swift
Swift provides some expressions in order to log, among others line number and method name:
Literal Type Value
#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.
For example:
func logFunctionName(string: String = #function) {
print(string)
}
func myFunction() {
logFunctionName() // Prints "myFunction()".
}
For more information, check the official documentation
Finding parent caller file, line number, and function in Swift
You can use default argument values. It would look something like this:
func formatError(_ except: Error,
function: StaticString = #function,
file: StaticString = #file,
line: UInt = #line) -> String {
return "[\(file).\(line):\(function)] ERROR: \(except.localizedDescription)"
}
How can I find a line number in Xcode?
You can go to:
Xcode > Preferences > Text Editing
then tick "Line numbers". Go to your method and you'll see the appropriate line number shown in the left-hand border of the text editor.
How to print out the method name and line number and conditionally disable NSLog?
Here are some useful macros around NSLog I use a lot:
#ifdef DEBUG
# define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#else
# define DLog(...)
#endif
// ALog always displays output regardless of the DEBUG setting
#define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
The DLog macro is used to only output when the DEBUG variable is set (-DDEBUG in the projects's C flags for the debug confirguration).
ALog will always output text (like the regular NSLog).
The output (e.g. ALog(@"Hello world") ) will look like this:
-[LibraryController awakeFromNib] [Line 364] Hello world
How can I access the current line number in objective-c?
Here you are a little piece of useful code:
#define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
Related Topics
Draw Scenekit Object Between Two Points
How to Check Is a String or Number
Use Resources in Unit Tests with Swift Package Manager
What Versions of Swift Are Supported by What Versions of Xcode
Understanding Swift 2.2 Selector Syntax - #Selector()
How to Save Data from Cloud Firestore to a Variable in Swift
Try, Try! & Try? What's the Difference, and When to Use Each
Alamofire Asynchronous Completionhandler For Json Request
Determining If Swift Dictionary Contains Key and Obtaining Any of Its Values
How to Silence a Warning in Swift
Add "For In" Support to Iterate Over Swift Custom Classes
Filter Array of [Anyobject] in Swift
How to Include .Swift File from Other .Swift File in an Immediate Mode
How to Set Collection View's Cell Size via the Auto Layout
The Strange Behaviour of Swift's Anyobject
Navigationview Pops Back to Root, Omitting Intermediate View