How to identify calling method from a called method in swift











up vote
0
down vote

favorite












here is a scenario



func callingMethod_A {
self.someCalculation()
}

func callingMethod_B{
self.someCalculation()
}

func someCalculation{
//how to find who called this method? is it callingMethod_A or _B at runtime?
//bla bla
}


how can we get the method name that called it during run time.

thank you.










share|improve this question
























  • pass the argument to identify the method
    – Anbu.karthik
    Nov 8 at 13:09










  • Add argument, or if it's just for debug purpose, look at the callstack.
    – Larme
    Nov 8 at 13:10










  • Larme, how do you look at the call stack in code? Is there a way to get the function signature from the call stack?
    – Duncan C
    Nov 8 at 13:31










  • While passing the "caller ID" into a method works, there may well be a better way (delegate, return) to accomplish what you want. Why does someCalculation need to know if callingMethod_A or callingMethod_B called it? What happens when you decide to add callingMethod_C or change callingMethod_A to be callingMethod_A1`?
    – dfd
    Nov 8 at 13:31















up vote
0
down vote

favorite












here is a scenario



func callingMethod_A {
self.someCalculation()
}

func callingMethod_B{
self.someCalculation()
}

func someCalculation{
//how to find who called this method? is it callingMethod_A or _B at runtime?
//bla bla
}


how can we get the method name that called it during run time.

thank you.










share|improve this question
























  • pass the argument to identify the method
    – Anbu.karthik
    Nov 8 at 13:09










  • Add argument, or if it's just for debug purpose, look at the callstack.
    – Larme
    Nov 8 at 13:10










  • Larme, how do you look at the call stack in code? Is there a way to get the function signature from the call stack?
    – Duncan C
    Nov 8 at 13:31










  • While passing the "caller ID" into a method works, there may well be a better way (delegate, return) to accomplish what you want. Why does someCalculation need to know if callingMethod_A or callingMethod_B called it? What happens when you decide to add callingMethod_C or change callingMethod_A to be callingMethod_A1`?
    – dfd
    Nov 8 at 13:31













up vote
0
down vote

favorite









up vote
0
down vote

favorite











here is a scenario



func callingMethod_A {
self.someCalculation()
}

func callingMethod_B{
self.someCalculation()
}

func someCalculation{
//how to find who called this method? is it callingMethod_A or _B at runtime?
//bla bla
}


how can we get the method name that called it during run time.

thank you.










share|improve this question















here is a scenario



func callingMethod_A {
self.someCalculation()
}

func callingMethod_B{
self.someCalculation()
}

func someCalculation{
//how to find who called this method? is it callingMethod_A or _B at runtime?
//bla bla
}


how can we get the method name that called it during run time.

thank you.







ios swift






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 13:12









Anbu.karthik

59.1k15121111




59.1k15121111










asked Nov 8 at 13:03









Pavan Kumar

63




63












  • pass the argument to identify the method
    – Anbu.karthik
    Nov 8 at 13:09










  • Add argument, or if it's just for debug purpose, look at the callstack.
    – Larme
    Nov 8 at 13:10










  • Larme, how do you look at the call stack in code? Is there a way to get the function signature from the call stack?
    – Duncan C
    Nov 8 at 13:31










  • While passing the "caller ID" into a method works, there may well be a better way (delegate, return) to accomplish what you want. Why does someCalculation need to know if callingMethod_A or callingMethod_B called it? What happens when you decide to add callingMethod_C or change callingMethod_A to be callingMethod_A1`?
    – dfd
    Nov 8 at 13:31


















  • pass the argument to identify the method
    – Anbu.karthik
    Nov 8 at 13:09










  • Add argument, or if it's just for debug purpose, look at the callstack.
    – Larme
    Nov 8 at 13:10










  • Larme, how do you look at the call stack in code? Is there a way to get the function signature from the call stack?
    – Duncan C
    Nov 8 at 13:31










  • While passing the "caller ID" into a method works, there may well be a better way (delegate, return) to accomplish what you want. Why does someCalculation need to know if callingMethod_A or callingMethod_B called it? What happens when you decide to add callingMethod_C or change callingMethod_A to be callingMethod_A1`?
    – dfd
    Nov 8 at 13:31
















pass the argument to identify the method
– Anbu.karthik
Nov 8 at 13:09




pass the argument to identify the method
– Anbu.karthik
Nov 8 at 13:09












Add argument, or if it's just for debug purpose, look at the callstack.
– Larme
Nov 8 at 13:10




Add argument, or if it's just for debug purpose, look at the callstack.
– Larme
Nov 8 at 13:10












Larme, how do you look at the call stack in code? Is there a way to get the function signature from the call stack?
– Duncan C
Nov 8 at 13:31




Larme, how do you look at the call stack in code? Is there a way to get the function signature from the call stack?
– Duncan C
Nov 8 at 13:31












While passing the "caller ID" into a method works, there may well be a better way (delegate, return) to accomplish what you want. Why does someCalculation need to know if callingMethod_A or callingMethod_B called it? What happens when you decide to add callingMethod_C or change callingMethod_A to be callingMethod_A1`?
– dfd
Nov 8 at 13:31




While passing the "caller ID" into a method works, there may well be a better way (delegate, return) to accomplish what you want. Why does someCalculation need to know if callingMethod_A or callingMethod_B called it? What happens when you decide to add callingMethod_C or change callingMethod_A to be callingMethod_A1`?
– dfd
Nov 8 at 13:31












2 Answers
2






active

oldest

votes

















up vote
1
down vote













I worked out a way to do this, for Swift code anyway:



Define a String parameter callingFunction and give it a default value of #function. Do not pass anything from the caller and the compiler provides the calling function name.



Building on @Anu.Krthik's answer:



func someCalculation (parameter: String, callingMethod: String = #function ) {
print("In `(#function)`, called by `(callingMethod)`")
}

func foo(string: String) {
someCalculation(parameter: string)
}

foo(string: "bar")


The above prints



In `someCalculation(parameter:callingMethod:)`, called by `foo(string:)`


However, beware that this technique can be subverted if the caller provides a value for the callingFunction parameter. if you call it with:



func foo(string: String) {
someCalculation(parameter: string, callingMethod: "bogusFunctionName()")
}


You get the output



In `someCalculation(parameter:callingMethod:)`, called by `bogusFunctionName()`


instead.






share|improve this answer




























    up vote
    1
    down vote













    You can use Thread.callStackSymbols like this



    func callingMethod_A() {
    self.someCalculation()
    }

    func callingMethod_B(){
    self.someCalculation()
    }

    func someCalculation(){
    let origin = Thread.callStackSymbols
    print(origin[0])
    print(origin[1])
    }





    share|improve this answer





















      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',
      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
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53208339%2fhow-to-identify-calling-method-from-a-called-method-in-swift%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








      up vote
      1
      down vote













      I worked out a way to do this, for Swift code anyway:



      Define a String parameter callingFunction and give it a default value of #function. Do not pass anything from the caller and the compiler provides the calling function name.



      Building on @Anu.Krthik's answer:



      func someCalculation (parameter: String, callingMethod: String = #function ) {
      print("In `(#function)`, called by `(callingMethod)`")
      }

      func foo(string: String) {
      someCalculation(parameter: string)
      }

      foo(string: "bar")


      The above prints



      In `someCalculation(parameter:callingMethod:)`, called by `foo(string:)`


      However, beware that this technique can be subverted if the caller provides a value for the callingFunction parameter. if you call it with:



      func foo(string: String) {
      someCalculation(parameter: string, callingMethod: "bogusFunctionName()")
      }


      You get the output



      In `someCalculation(parameter:callingMethod:)`, called by `bogusFunctionName()`


      instead.






      share|improve this answer

























        up vote
        1
        down vote













        I worked out a way to do this, for Swift code anyway:



        Define a String parameter callingFunction and give it a default value of #function. Do not pass anything from the caller and the compiler provides the calling function name.



        Building on @Anu.Krthik's answer:



        func someCalculation (parameter: String, callingMethod: String = #function ) {
        print("In `(#function)`, called by `(callingMethod)`")
        }

        func foo(string: String) {
        someCalculation(parameter: string)
        }

        foo(string: "bar")


        The above prints



        In `someCalculation(parameter:callingMethod:)`, called by `foo(string:)`


        However, beware that this technique can be subverted if the caller provides a value for the callingFunction parameter. if you call it with:



        func foo(string: String) {
        someCalculation(parameter: string, callingMethod: "bogusFunctionName()")
        }


        You get the output



        In `someCalculation(parameter:callingMethod:)`, called by `bogusFunctionName()`


        instead.






        share|improve this answer























          up vote
          1
          down vote










          up vote
          1
          down vote









          I worked out a way to do this, for Swift code anyway:



          Define a String parameter callingFunction and give it a default value of #function. Do not pass anything from the caller and the compiler provides the calling function name.



          Building on @Anu.Krthik's answer:



          func someCalculation (parameter: String, callingMethod: String = #function ) {
          print("In `(#function)`, called by `(callingMethod)`")
          }

          func foo(string: String) {
          someCalculation(parameter: string)
          }

          foo(string: "bar")


          The above prints



          In `someCalculation(parameter:callingMethod:)`, called by `foo(string:)`


          However, beware that this technique can be subverted if the caller provides a value for the callingFunction parameter. if you call it with:



          func foo(string: String) {
          someCalculation(parameter: string, callingMethod: "bogusFunctionName()")
          }


          You get the output



          In `someCalculation(parameter:callingMethod:)`, called by `bogusFunctionName()`


          instead.






          share|improve this answer












          I worked out a way to do this, for Swift code anyway:



          Define a String parameter callingFunction and give it a default value of #function. Do not pass anything from the caller and the compiler provides the calling function name.



          Building on @Anu.Krthik's answer:



          func someCalculation (parameter: String, callingMethod: String = #function ) {
          print("In `(#function)`, called by `(callingMethod)`")
          }

          func foo(string: String) {
          someCalculation(parameter: string)
          }

          foo(string: "bar")


          The above prints



          In `someCalculation(parameter:callingMethod:)`, called by `foo(string:)`


          However, beware that this technique can be subverted if the caller provides a value for the callingFunction parameter. if you call it with:



          func foo(string: String) {
          someCalculation(parameter: string, callingMethod: "bogusFunctionName()")
          }


          You get the output



          In `someCalculation(parameter:callingMethod:)`, called by `bogusFunctionName()`


          instead.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 13:30









          Duncan C

          90.6k13112192




          90.6k13112192
























              up vote
              1
              down vote













              You can use Thread.callStackSymbols like this



              func callingMethod_A() {
              self.someCalculation()
              }

              func callingMethod_B(){
              self.someCalculation()
              }

              func someCalculation(){
              let origin = Thread.callStackSymbols
              print(origin[0])
              print(origin[1])
              }





              share|improve this answer

























                up vote
                1
                down vote













                You can use Thread.callStackSymbols like this



                func callingMethod_A() {
                self.someCalculation()
                }

                func callingMethod_B(){
                self.someCalculation()
                }

                func someCalculation(){
                let origin = Thread.callStackSymbols
                print(origin[0])
                print(origin[1])
                }





                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  You can use Thread.callStackSymbols like this



                  func callingMethod_A() {
                  self.someCalculation()
                  }

                  func callingMethod_B(){
                  self.someCalculation()
                  }

                  func someCalculation(){
                  let origin = Thread.callStackSymbols
                  print(origin[0])
                  print(origin[1])
                  }





                  share|improve this answer












                  You can use Thread.callStackSymbols like this



                  func callingMethod_A() {
                  self.someCalculation()
                  }

                  func callingMethod_B(){
                  self.someCalculation()
                  }

                  func someCalculation(){
                  let origin = Thread.callStackSymbols
                  print(origin[0])
                  print(origin[1])
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 8 at 14:06









                  Raul Mantilla

                  595




                  595






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53208339%2fhow-to-identify-calling-method-from-a-called-method-in-swift%23new-answer', 'question_page');
                      }
                      );

                      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







                      Popular posts from this blog

                      Schultheiß

                      Verwaltungsgliederung Dänemarks

                      Liste der Kulturdenkmale in Wilsdruff