-5

I am trying to have a dynamic swift program that at runtime will do some actions and convert a string into a method call.

Example:

Array = ["TestMethod()", "TestMethod2()"]

I want to essentially say

if the sky is blue{
  convertStringToMethodCall(Array[0])
}

The goal is for the TestMethod() to actually get called at runtime.

NOTE: This must be one in Swift 5.

rmaddy
  • 307,833
  • 40
  • 508
  • 550
logankilpatrick
  • 10,898
  • 3
  • 25
  • 71
  • This question has some good details, but it's out of date and the language has changed significantly since then. https://stackoverflow.com/questions/24245262/call-a-method-from-a-string-in-swift – logankilpatrick Jul 12 '19 at 21:37
  • I wonder what use case this is for ? – Sh_Khan Jul 12 '19 at 21:55
  • Read from a file, write some swift code, and have the swift code checking dynamically conditions, and if certain dynamic conditions pass, call a method that is in text form. – logankilpatrick Jul 12 '19 at 21:56
  • 1
    Methods cannot be "in text form" in Swift. – matt Jul 12 '19 at 22:02

1 Answers1

0

Based on the response of others and more digging, this cannot currently be done using Swift 5.x or later! If you too are trying t do this, you will need to find another solution.

logankilpatrick
  • 10,898
  • 3
  • 25
  • 71