How to get the expected value of #function or #line?
I have a function that contains #function
and #line
that returns a string:
func exportMessage(content: String) -> String {
if let fileURL = URL(string: #file) {
return "(fileURL.lastPathComponent): (#function) - Line: (#line)n(content)"
}
return "(#function) - Line: (#line)n(content)"
}
When trying to use exportMessage
above function in another function:
func doSomething() {
let result = exportMessage(content: "instance is nil")
print(result)
}
The output is:
MyPlayground.playground: exportMessage(content:) - Line: 5
instance is nil
It contains "exportMessage" but not "doSomething" as the name of the function (#function
). It is the same for the line number, it is line number in exportMessage()
but not in doSomething()
.
My expectation is to get the function name and the line number for doSomething()
, as:
MyPlayground.playground: doSomething() - Line: 12
instance is nil
Since I am aiming to use exportMessage
in many functions, I'd assume that it should refers to the caller function. Is it achievable? How?
swift
add a comment |
I have a function that contains #function
and #line
that returns a string:
func exportMessage(content: String) -> String {
if let fileURL = URL(string: #file) {
return "(fileURL.lastPathComponent): (#function) - Line: (#line)n(content)"
}
return "(#function) - Line: (#line)n(content)"
}
When trying to use exportMessage
above function in another function:
func doSomething() {
let result = exportMessage(content: "instance is nil")
print(result)
}
The output is:
MyPlayground.playground: exportMessage(content:) - Line: 5
instance is nil
It contains "exportMessage" but not "doSomething" as the name of the function (#function
). It is the same for the line number, it is line number in exportMessage()
but not in doSomething()
.
My expectation is to get the function name and the line number for doSomething()
, as:
MyPlayground.playground: doSomething() - Line: 12
instance is nil
Since I am aiming to use exportMessage
in many functions, I'd assume that it should refers to the caller function. Is it achievable? How?
swift
add a comment |
I have a function that contains #function
and #line
that returns a string:
func exportMessage(content: String) -> String {
if let fileURL = URL(string: #file) {
return "(fileURL.lastPathComponent): (#function) - Line: (#line)n(content)"
}
return "(#function) - Line: (#line)n(content)"
}
When trying to use exportMessage
above function in another function:
func doSomething() {
let result = exportMessage(content: "instance is nil")
print(result)
}
The output is:
MyPlayground.playground: exportMessage(content:) - Line: 5
instance is nil
It contains "exportMessage" but not "doSomething" as the name of the function (#function
). It is the same for the line number, it is line number in exportMessage()
but not in doSomething()
.
My expectation is to get the function name and the line number for doSomething()
, as:
MyPlayground.playground: doSomething() - Line: 12
instance is nil
Since I am aiming to use exportMessage
in many functions, I'd assume that it should refers to the caller function. Is it achievable? How?
swift
I have a function that contains #function
and #line
that returns a string:
func exportMessage(content: String) -> String {
if let fileURL = URL(string: #file) {
return "(fileURL.lastPathComponent): (#function) - Line: (#line)n(content)"
}
return "(#function) - Line: (#line)n(content)"
}
When trying to use exportMessage
above function in another function:
func doSomething() {
let result = exportMessage(content: "instance is nil")
print(result)
}
The output is:
MyPlayground.playground: exportMessage(content:) - Line: 5
instance is nil
It contains "exportMessage" but not "doSomething" as the name of the function (#function
). It is the same for the line number, it is line number in exportMessage()
but not in doSomething()
.
My expectation is to get the function name and the line number for doSomething()
, as:
MyPlayground.playground: doSomething() - Line: 12
instance is nil
Since I am aiming to use exportMessage
in many functions, I'd assume that it should refers to the caller function. Is it achievable? How?
swift
swift
edited Nov 21 '18 at 17:17
Ahmad F
asked Nov 21 '18 at 17:07
Ahmad FAhmad F
16.3k104181
16.3k104181
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
What you're proposing wouldn't really make sense, because it wouldn't be clear "which caller should it print"? Why doSomething
? Why not the caller of doSomething
? Or the caller of the caller? Ultimately, if this is how #function
and friends behaved, it would only ever print start
from libdyld.dylid
.
To achieve the behaviour you want, you have to manually pull in the #function
and #line
values from the parent context:
func exportMessage(
content: String,
callingFunction: String = #function,
callingLine: Int = #line
) -> String {
if let fileURL = URL(string: callingFunction) {
return "(fileURL.lastPathComponent): (callingFunction) - Line: (callingLine)n(content)"
}
return "(callingFunction) - Line: (callingLine)n(content)"
}
func doSomething() {
let result = exportMessage(content: "instance is nil")
print(result)
}
That's what I got when I checked such a method declaration: developer.apple.com/documentation/swift/1541112-assert. Thanks!
– Ahmad F
Nov 22 '18 at 8:06
add a comment |
Use #function
and #line
as default values in the method. Default values are evaluated at the point of call.
func exportMessage(content: String, file: String = #file, function: String = #function, line: Int = #line) -> String {
if let fileURL = URL(string: file) {
return "(fileURL.lastPathComponent): (function) - Line: (line)n(content)"
}
return "(function) - Line: (line)n(content)"
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417249%2fhow-to-get-the-expected-value-of-function-or-line%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
What you're proposing wouldn't really make sense, because it wouldn't be clear "which caller should it print"? Why doSomething
? Why not the caller of doSomething
? Or the caller of the caller? Ultimately, if this is how #function
and friends behaved, it would only ever print start
from libdyld.dylid
.
To achieve the behaviour you want, you have to manually pull in the #function
and #line
values from the parent context:
func exportMessage(
content: String,
callingFunction: String = #function,
callingLine: Int = #line
) -> String {
if let fileURL = URL(string: callingFunction) {
return "(fileURL.lastPathComponent): (callingFunction) - Line: (callingLine)n(content)"
}
return "(callingFunction) - Line: (callingLine)n(content)"
}
func doSomething() {
let result = exportMessage(content: "instance is nil")
print(result)
}
That's what I got when I checked such a method declaration: developer.apple.com/documentation/swift/1541112-assert. Thanks!
– Ahmad F
Nov 22 '18 at 8:06
add a comment |
What you're proposing wouldn't really make sense, because it wouldn't be clear "which caller should it print"? Why doSomething
? Why not the caller of doSomething
? Or the caller of the caller? Ultimately, if this is how #function
and friends behaved, it would only ever print start
from libdyld.dylid
.
To achieve the behaviour you want, you have to manually pull in the #function
and #line
values from the parent context:
func exportMessage(
content: String,
callingFunction: String = #function,
callingLine: Int = #line
) -> String {
if let fileURL = URL(string: callingFunction) {
return "(fileURL.lastPathComponent): (callingFunction) - Line: (callingLine)n(content)"
}
return "(callingFunction) - Line: (callingLine)n(content)"
}
func doSomething() {
let result = exportMessage(content: "instance is nil")
print(result)
}
That's what I got when I checked such a method declaration: developer.apple.com/documentation/swift/1541112-assert. Thanks!
– Ahmad F
Nov 22 '18 at 8:06
add a comment |
What you're proposing wouldn't really make sense, because it wouldn't be clear "which caller should it print"? Why doSomething
? Why not the caller of doSomething
? Or the caller of the caller? Ultimately, if this is how #function
and friends behaved, it would only ever print start
from libdyld.dylid
.
To achieve the behaviour you want, you have to manually pull in the #function
and #line
values from the parent context:
func exportMessage(
content: String,
callingFunction: String = #function,
callingLine: Int = #line
) -> String {
if let fileURL = URL(string: callingFunction) {
return "(fileURL.lastPathComponent): (callingFunction) - Line: (callingLine)n(content)"
}
return "(callingFunction) - Line: (callingLine)n(content)"
}
func doSomething() {
let result = exportMessage(content: "instance is nil")
print(result)
}
What you're proposing wouldn't really make sense, because it wouldn't be clear "which caller should it print"? Why doSomething
? Why not the caller of doSomething
? Or the caller of the caller? Ultimately, if this is how #function
and friends behaved, it would only ever print start
from libdyld.dylid
.
To achieve the behaviour you want, you have to manually pull in the #function
and #line
values from the parent context:
func exportMessage(
content: String,
callingFunction: String = #function,
callingLine: Int = #line
) -> String {
if let fileURL = URL(string: callingFunction) {
return "(fileURL.lastPathComponent): (callingFunction) - Line: (callingLine)n(content)"
}
return "(callingFunction) - Line: (callingLine)n(content)"
}
func doSomething() {
let result = exportMessage(content: "instance is nil")
print(result)
}
answered Nov 21 '18 at 17:25
AlexanderAlexander
31.2k54878
31.2k54878
That's what I got when I checked such a method declaration: developer.apple.com/documentation/swift/1541112-assert. Thanks!
– Ahmad F
Nov 22 '18 at 8:06
add a comment |
That's what I got when I checked such a method declaration: developer.apple.com/documentation/swift/1541112-assert. Thanks!
– Ahmad F
Nov 22 '18 at 8:06
That's what I got when I checked such a method declaration: developer.apple.com/documentation/swift/1541112-assert. Thanks!
– Ahmad F
Nov 22 '18 at 8:06
That's what I got when I checked such a method declaration: developer.apple.com/documentation/swift/1541112-assert. Thanks!
– Ahmad F
Nov 22 '18 at 8:06
add a comment |
Use #function
and #line
as default values in the method. Default values are evaluated at the point of call.
func exportMessage(content: String, file: String = #file, function: String = #function, line: Int = #line) -> String {
if let fileURL = URL(string: file) {
return "(fileURL.lastPathComponent): (function) - Line: (line)n(content)"
}
return "(function) - Line: (line)n(content)"
}
add a comment |
Use #function
and #line
as default values in the method. Default values are evaluated at the point of call.
func exportMessage(content: String, file: String = #file, function: String = #function, line: Int = #line) -> String {
if let fileURL = URL(string: file) {
return "(fileURL.lastPathComponent): (function) - Line: (line)n(content)"
}
return "(function) - Line: (line)n(content)"
}
add a comment |
Use #function
and #line
as default values in the method. Default values are evaluated at the point of call.
func exportMessage(content: String, file: String = #file, function: String = #function, line: Int = #line) -> String {
if let fileURL = URL(string: file) {
return "(fileURL.lastPathComponent): (function) - Line: (line)n(content)"
}
return "(function) - Line: (line)n(content)"
}
Use #function
and #line
as default values in the method. Default values are evaluated at the point of call.
func exportMessage(content: String, file: String = #file, function: String = #function, line: Int = #line) -> String {
if let fileURL = URL(string: file) {
return "(fileURL.lastPathComponent): (function) - Line: (line)n(content)"
}
return "(function) - Line: (line)n(content)"
}
answered Nov 21 '18 at 17:25
Rob NapierRob Napier
200k28294421
200k28294421
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417249%2fhow-to-get-the-expected-value-of-function-or-line%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown