convert an array of string values to use as a formulas - iphone

I am new to coding and objective C so thanks for the help in advance.
I have a .plist file containing an array of strings filled with formulas such as
*5.3
/2
-10.5
I am able to retrieve these string values from the .plist file but I am getting a little stuck trying to append these string formulas to existing variables with the hopes of returning a converted number. For example I would like to use my variable 7 with the formula *5.3 and return 37.1
7 *5.3 -> 37.1
Any help would be greatly appreciated

Appending the string to a variable is straightforward; it can be accomplished with something like this:
NSString *equation = [NSString stringWithFormat:#"%d%#", variable, plistEntry];
You'll run into problems when you want to evaluate this equation, however. This SO question discusses expression evaluation in Objective-C. Dave DeLong's answer links to a couple of libraries that you may want to look into: DDMathParser and GCMathParser.

This can't be done as-is. You'll need one of the many free expression evaluators (probably in C) that float around on the web.
See this SO question.

Related

How can I save a string array to PlayerPrefs in Unity?

I have an array and I would like to save it to PlayerPrefs. I heard, I can do this:
PlayerPrefs.SetStringArray('title', anArray);
but for some reason it does not work.
Maybe I'm not using some library like using UnityEngine.PlayerPrefs;?
Can someone help me?
Thanks in advance
You can't. PlayerPrefs doesn't support arrays.
But you could use a special separator and do e.g.
PlayerPrefs.SetString("title", string.Join("###", anArray));
and then for reading use
var anArray = PlayerPrefs.SetString("title").Split(new []{"###"}, StringSplitOptions.None);
Or if you know the content and in particular which character is never used you could also use a single char e.g.
PlayerPrefs.SetString("title", string.Join("/n", anArray));
and then for reading use
var anArray = PlayerPrefs.SetString("title").Split('/n');
Yes as TEEBQNE mentioned there is PlayerPrefsX.cs which might be the source of the confusion.
I would NOT recommend it though! It simply converts all the different input types into byte[] and from there to Base64 strings.
That might be cool and all for int[], bool[], etc. But for string[] this is absolutely inefficient since the Base64 bytes representation of a string is way longer than the string itself!
It might be a valid alternative though if you can not rely on your strings contents and you can not be sure that your separator sequence is never actually a content of any string.

Evaluate string as input keyword in matlab

How to evaluate a string which could be changed dynamically in code? for example:
A=rand(60, 60);
RangeC='10:end,:';
B=A(RangeC);
I know this is quite easy for others, but I have struggled for hours! Thanks in advance!
You can use the eval function but I would suggest to seperate RangeC in two variables like the example below. Also end won't be able to be evaluated so you can use size instead.
A=rand(60, 60);
RangeC1='10:size(A,1)';
RangeC2='1:size(A,2)';
B=A(eval(RangeC1), eval(RangeC2));

Swift: Converting a string into a variable name

I have variables with incremented numbers within, such as row0text, row1text, row2text, etc.
I've figured out how to dynamically create string versions of those variable names, but once I have those strings, how can I use them as actual variable names rather than strings in my code?
Example:
var row3text = "This is the value I need!"
var firstPart = "row"
var rowNumber = 3
var secondPart = "text"
var together = (firstPart+String(rowNumber)+secondPart)
// the below gives me the concatenated string of the three variables, but I'm looking for a way to have it return the value set at the top.
println (together)
Once I know how to do this, I'll be able to iterate through those variables using a for loop; it's just that at the moment I'm unsure of how to use that string as a variable name in my code.
Thanks!
Short Answer: There is no way to do this for good reason. Use arrays instead.
Long Answer:
Essentially you are looking for a way to define an unknown number of variables that are all linked together by their common format. You are looking to define an ordered set of elements of variable length. Why not just use an array?
Arrays are containers that allow you to store an ordered set or list of elements and access them by their ordered location, which is exactly what you're trying to do. See Apple's Swift Array Tutorial for further reading.
The advantage of arrays is that they are faster, far more convenient for larger sets of elements (and probably the same for smaller sets as well), and they come packaged with a ton of useful functionality. If you haven't worked with arrays before it is a bit of a learning curve but absolutely worth it.

Single specific string replace method Objective C

I wanted to know if theres a single method or way that will help me replace strings for specific characters.
like MALE - M
FEMALE - F
CHILD - P
The longer way out is this..
[str stringByreplacingOccurencesOfString:#"MALE" withString:#"M"];
[str stringByreplacingOccurencesOfString:#"FEMALE" withString:#"F"];
[str stringByreplacingOccurencesOfString:#"CHILD" withString:#"P"];
I was wondering if theres another way in which i can reduce lines of code here, specially when there are alots of things to replace.
thanks.
this is for iPhone OS.
No, but it shouldn't take more than 5-10 minutes to write a method that takes an array or varargs to do it for you.

Newbie Objective C developer question

I have been looking everywhere for an answer to this question - perhaps I'm looking in the wrong places. Also, I'm brand new to Objective C although I have around 10 years of experience as a developer.
for this code:
[receiver makeGroup:group, memberOne, memberTwo, memberThree];
what would the method definition look like?
- (void)makeGroup:(Group *)g, (NSString *)memberOne, ...?
Thanks for any help you can provide. I know this is probably very simple...
Thanks,
R
It looks like you have a method that can take a variable number of arguments. If that's the case, the definition would look something like:
- (void)makeGroup:(Group *)g, ...;
Check out NSString's stringWithFormat: or NSArray's arrayWithObjects: methods for examples.
Edit: Upon further documentation reading, it seems that you are looking at the exact example that's in the Objective-C 2.0 documentation. The declaration you're looking for is right at the bottom of page 36.
You can receive an infinte number of arguments with an ellipsis (...). Check this for further details!
It would make more sense to have the members as a separate array argument, like -(void)makeGroup:(Group *)g members:(NSArray *)members. If you must do varargs (which is a pain), it should be written like -(void)makeGroup:(Group *)g members:(NSString *)firstMember, ....
Since I this is trying to figure out how an example method from the documentation would be declared, it would be like this:
- (void)makeGroup:(id)group, ...
Then you would start up the varags machinery with the group argument and use it to find the other arguments.
either you're looking for MrHen's answer if you're seeking to do your own class method or if you want to do them separately you write the following into your header file:
-(void)makeGroup:(Group *)g;
-(NSString *)memberOne;
EDIT: I answered the wrong question. Ignore this.
The correct way to do this is:
-(void)makeGroup:(Group *)g memberOne:(NSString *)memberOne memberTwo:(NSString *)memberTwo memberThree:(NSString *)memberThree {
...
}
The call will look like this:
[receiver makeGroup:group memberOne:memberOne memberTwo:memberTwo memberThree:memberThree];