I am building an app where a user can solve 30+ different math problems. Each problem has a set of random generated variables (nums) and has its own unique formula to calculate the correct result.
I am planning to use json to store these 30+ different math tasks. My question is the following:
Is there any way to store functions in json (even as string) and then decode them as functions in swift? I need it to avoid typing 30+ functions directly in swift file.
Or maybe there is a better way to store data and functions for my app and I just do not know about it.
Please find below JSON with one problem as an example.
Each element from nums is generated randomly in swift within the range [minValue...maxValue]. After that I use nums to calculate the correct result. In this case formula will be
nums[0].value*(1+nums[1].value)^nums[2].value
{
"Rtexts":[
{
"value":["Now", "As of now", "Currently", "In Year 0"],
"sequence":1
},
{
"value":["EBITDA", "Revenue", "Sales", "Profit", "COGS", "SG&A", "Overheads", "Variable Cost", "Fixed Cost", "Income", "Selling Expense", "Administrative Expense", "Gross profit", "OPEX", "CAPEX", "Capital expenditures", "R&D Expense", "Cost of goods sold", "Labor Cost", "Payroll Expense"],
"sequence":2
},
{
"value":["the company", "the logistics company", "the chain of shopping malls", "the food delivery service", "the telecom operator", "the carsharing company", "the distributor of B2B IT services", "th fashion retailer", "the oil&gas company", "the startup", "the pharmaceutical company", "the FMCG company", "the electric utility company", "the online marketplace", "the producer of aluminum", "the automotive manufacturing company", "the hardware producer", "the manufacturer of consumer electronics", "the company Pollofret Limited", "the company Montos&Brothers", "the company BeertyCo", "company Farenty Enterprise", "the company Venderatelli", "the company Anita and Co", "the company Delaveli Corporated", "the competitor's business", "the acquired company", "the company FT&T"],
"sequence":4
},
{
"value":["grow", "increase", "be growing","be increasing"],
"sequence":8
},
{
"value":["reach approximately", "be around", "equal approximately", "increase up to"],
"sequence":14
}
],
"texts":[
{
"texttoShow":"of",
"sequence":3
},
{
"texttoShow":"is",
"sequence":5
},
{
"texttoShow":"mln dollars. It will",
"sequence":7
},
{
"texttoShow":"at the average annual rate of",
"sequence":9
},
{
"texttoShow":"After",
"sequence":11
},
{
"texttoShow":"years it will",
"sequence":13
},
{
"texttoShow":"mln dollars.",
"sequence":16
}
],
"nums":[
{
"minValue":10,
"maxValue":99,
"denominator":1,
"sequence":6
},
{
"minValue":5,
"maxValue":150,
"denominator":10,
"sequence":10
},
{
"minValue":3,
"maxValue":10,
"denominator":1,
"sequence":12
}
],
"answer":{
"texttoShow":"_____",
"sequence":15
}
}
question from:
https://stackoverflow.com/questions/65845141/how-to-store-functions-in-json-or-other-way-and-parse-them-in-swift