iOS comparing NSDictionaries in Arrays - string

I'm trying to find the index of an NSDictionary in an Array with NSDictionaries, but having problems with the encoding of the two.
Array a: is downloaded from a webserver (as a csv-string) and then converted into an Array of dictionaries.
Array b: is loaded with [NSArray arrayWithContentsOfFile] from a plist.
The dictionaries and arrays look good, but [indexOfObject] doesn't give me any results.
By outputting the data to the console, I found out that German Umlauts are encoded differently.
Array a: shows the character «ä» as \U00c3\U00a4
Array b: shows the character «ä» as \U00e4
So I guess that's why the function returns a NSNotFound.
I tried to cast the strings in many different ways, with no result...
Interestingly, the function used to work...
Any Ideas?
[EDIT]
Well, it turns out I didn't process the data coming from the server correctly.
I had to change this
NSString *dataStr=[[NSString alloc] initWithData:self.receivedData encoding:NSASCIIStringEncoding];
into this:
NSString *dataStr=[[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding];

Related

Nodejs - Read values of array as number instead of string

I created an array inside my own class in nodejs to store real values.
this.values = new Array();
and almost every second a new value is pushed to this array.
this.values.push(Number(dataset.c).toFixed(4));
Finaly I have a function where I need to provide the array in the argument list to do bollinger band calculation. This function need an array filled with real or integer values but in my array every value is stored as an string.
Is it possible to change this storage behaviour or how can I 'typecast' all the values in this array?
Best regards
Found a solution using the map-Function.
this.values.map(Number)
did the job.

Unity: Comparing strings doesn't seem to work in this case?

I'm trying to compare to strings in a function to be able to do stuff if it's true. However, for some reason the two strings I'm comparing are never equal to eachother.
I am creating a new string in a function, getting data from a Json and then comparing that string with another string in another class. This doesn't work for some reason. The Debug shows that both strings are equal, but the if-statement returns false.
I created two other string variables (hello & hello2) with the same value ("Hello") and this time it's comparing correctly.
Check the images below. What am I doing wrong?
As you can see in the console, both strings have the same value:
Image 1. Here's where I create the string (zoneId).
Image 2. Further down in the same function, same for-loop.
Here's where I'm trying to compare the string created in this function with another string from another class.
Image 3. As you can see in the console it's looping through the jsonArray. But it's returning false even though it's clear that both strings have the same value.
Image 4 and 5. Here I am testing with two other strings inside the function and they are working fine.
Does it has something to do calling a string from another class?
Here's how my other string in userInfo is set up:
public string userID { get; private set; }
In Image 3, is there an additional space before the second 2?
How are you processing Main.Instance.userInfo.zoneID?
You need to use string.Equals for string comparison instead of operator == or !=.
Are string.Equals() and == operator really same?

How to convert a string array into floats in PHP

I'm selecting some Strings from my SQLite DB and store them in an Array. Now I want to plot the values with the framework "Razorflow". I think it is only possible if the values of the array are floats, am I wrong?
In my DB I'm storing temperature and humidity of 12 different sensor nodes, in this form:
id|temperature|humidity|...|...
1 | 22.50C| 47.50%|...|...
..
I heared something about the floatval()-function, I also heared that this function is not made for objects like Arrays. Is there a simple solution? I'm very new to PHP :-D
Depends on how you're getting the values back from the database.
id|temperature|humidity|...|...
Sounds like a string to me, so i would first explode it into an array and then iterate the array casting floatval into every element.
What you do after that process depends on you.
EDIT:
If your query is:
$humi = $database->query((SELECT humidity FROM Measurement WHERE topic_hum='WSN9/humi'
You will get back only 1 column (humidity) and 0 or more rows depending on your database. Let's say it's only 1 for now:
$resul = mysql_query($humi,$link);
$rows = mysql_fetch_array($resul);
$myarray = explode("|", $rows["humidity"]);
This should give us an array called myarray containing X elements each with a "single" string part value. So we can iterate over it and parse it. There is also the shorthand "array_map" to iterate over an array using a callback function over each element and returning the value as an array:
$myparsedarray = array_map('floatval', $myarray)
Now you have an array with only float values (and errors maybe, check your data!)

Processing Split (server)

I am doing 2player game and when I get informations from server, it's in format "topic;arg1;arg2" so if I am sending positions it's "PlayerPos;x;y".
I then use split method with character ";".
But then... I even tried to write it on screen "PlayerPos" was written right, but it cannot be gained through if.
This is how I send info on server:
server.write("PlayerPos;"+player1.x+";"+player1.y);
And how I accept it on client:
String Get=client.readString();
String [] Getted = split(Get, ';');
fill(0);
text(Get,20,20);
text(Getted[0],20,40);
if(Getted[0]=="PlayerPos"){
text("HERE",20,100);
player1.x=parseInt(Getted[1]);
player1.x=parseInt(Getted[2]);
}
It writes me "PlayerPos;200;200" on screen, even "PlayerPos" under it. But it never writes "HERE" and it never makes it into the if.
Where is my mistake?
Don't use == when comparing String values. Use the equals() function instead:
if(Getted[0].equals("PlayerPos")){
From the Processing reference:
To compare the contents of two Strings, use the equals() method, as in if (a.equals(b)), instead of if (a == b). A String is an Object, so comparing them with the == operator only compares whether both Strings are stored in the same memory location. Using the equals() method will ensure that the actual contents are compared. (The troubleshooting reference has a longer explanation.)

Locating and removing an esc sequence objective-c

I'm trying to remove an escape sequence (\p) from selected strings (for reuse in other places in my code). I want to create a separate object that holds that cleaned up string. Like this
(pseudo code)
NSString *stringWithEscSequence = anEdgewoodSecretPlace.placeName;
NSString *stringWithoutEscSequence = [removeTheEscSequenceP stringWithEscSequence];
(where removeTheEscSequenceP is what I'm trying to figure out how to do)
....
I played with NSString formats and I found related questions at
Position of a character NSString
and
how to replace one substring with another in Objective C?
I can break out the substring that follows the escape sequence use ideas from the second posting. So I thought then I should try to locate the actual index of the escape sequence. But I can't figure out how to do that. Also I think there is probably a less messy, more straightforward way to do this, that I'm not thinking of.
I was playing around with the first posting ideas, though I knew that I was in a kind of primitive mode. But as soon as I attempt to replace "ABCD" with "\". I get an error 'Missing terminating """ = except if I put the ABCD string back in I'm not missing any quotes. I think it's because of the special nature ofo the \ character.
Even if I could figure out how to search for the "\" and then a "p" following that, I don't think that's really the best way to approach this
NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:#"ABCD"];
NSRange range2 = [testCleanString rangeOfCharacterFromSet:charSet];
if (range2.location == NSNotFound) {
NSLog (#"Didn't find esc sequence");
} else {
NSLog (#"range.location is %lu",(unsigned long)range2.location );
}
You could try with this:
NSString* stringWithoutEscSequence = [stringWithEscSequence stringByReplacingOccurrencesOfString:#"stringToRemove" withString:#"replacement"];
If you want to replace multiple characters you could do:
NSCharacterSet* toRemove = [NSCharacterSet characterSetWithCharactersInString:#"\n\p\t"];
NSString* stringWithoutEscSequence = [[stringWithEscSequence componentsSeparatedByCharactersInSet:toRemove] componentsJoinedByString: #""];

Resources