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.
Related
I have an algorithm whose intermediate step is to replace a substring with another substring. To be precise I have a string HBIN_NEW and I have another string P. I want to replace every 6th,7th,8th element of the string HREP with the 1st,2nd,3rd element of PBIN_NEW. For this i have written the code
For example If PBIN_NEW='1111111101010101' and HBIN_NEW='1111100010101010'
then the new string HREP
should be HREP='1111111110101101'
for k=1:8:262144*8
HREP=strrep(HBIN_NEW,HBIN_NEW(k+5:k+7),PBIN_NEW(k:k+2));
end
Is this code correct to implement the above idea. And if yes, it is taking a long time do this replacement scheme, can somebody suggest some optimized way of doing this.
The wording on the question is still a bit awkward, and I'm not exactly sure how to get the example HREP given the wording, but most likely strrep is overkill for what it sounds like you are trying to do. A simple loop with assignments would be fine:
HREP = HBIN_NEW;
for k=1:8:length(HBIN_NEW)
HREP(k+5:k+7) = PBIN_NEW(k:k+2);
end
Often times though it can be better to just enumerate the position assignments and avoid the loop. So instead you have something like this:
HREP = HBIN_NEW;
HREP(6:8:end) = PBIN_NEW(1:8:end);
HREP(7:8:end) = PBIN_NEW(2:8:end);
HREP(8:8:end) = PBIN_NEW(3:8:end);
I think that does what you want, or should get you close enough ...
Finally, a bit of unsolicited style advice. Although Matlab doesn't have a very strict code style guide, most likely the use of all caps with underscores is not the best way of naming your variables. I personally prefer lowercase with underscores, e.g. pbin_new and only use capitalized words for constants ...
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.
With Guava, hashing can be as simple as
byte[] byteHash = Hashing.md5().hashBytes(aByteArray).asBytes();
but seemingly only as all you want is a byte[] (possibly converted to a hex string), or a single int or long. But in one place I need two longs and in another one I need five int from sha1.
I can see some solutions like reading from new DataInputStream(new ByteArrayInputStream(byteHash)), using a ByteBuffer, or converting manually from the byte[]. However, all of them are extremely ugly (e.g. swallowing an impossible IOException) and long (and also inefficient, but this doesn't bother me here).
So is there any simple way to extract multiple (non-byte) values from Guava's HashCode?
There's nothing built in to HashCode for this, no.
Doing what you need with ByteBuffer seems really easy though, and neither long nor especially inefficient:
ByteBuffer buf = ByteBuffer.wrap(byteHash);
long l1 = buf.getLong();
long l2 = buf.getLong();
(I suppose an asReadOnlyByteBuffer() method could avoid the need for cloning a byte array, but I don't know if that's really necessary.)
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.
In an class header I have seen something like this:
enum {
kAudioSessionProperty_PreferredHardwareSampleRate = 'hwsr', // Float64
kAudioSessionProperty_PreferredHardwareIOBufferDuration = 'iobd' // Float32
};
Now I wonder what data type such an kAudioSessionProperty_PreferredHardwareSampleRate actually is?
I mean this looks like plain old C, but in Objective-C I would write #"hwsr" if I wanted to make it a string.
I want to pass such an "constant" or "enum thing" as argument to an method.
This converts to an UInt32 enum value using the ASCII value of each of the entries. This style have been around for a long time in Mac OS headers.
'hwsr' has the same value as if you had written 0x68777372, but is a lot more reader friendly. If you used the #"hwsr" style instead you would need more than 4 bytes to represent the same.
The advantage of using this style is that you are actually able to quickly identify the content of a raw data stream if you can see the ASCII values of it.