-4

I want to create iOS app, which will show me an actual temperature from my API. I created API, which shows me, which IDs of sensors are available in this format:

1;2;100

It's means, I have 3 sensors with ID 1, 2 and 100... When I have this I can get actual temperature from api, using this string http://someurl.com/api/actual/ID when ID is an ID of sensor which I want... It gives me this:

Name_of_sensor;02/26/2015;21:16;22.88

When: - Name of sensor is name of sensor of course - 02/26/2015 is a date when was this temperature captured - 21:16 is a time when was this temperature captured - 22.88 is actual temp in C

How to use the first string to get available IDs and after that get values of all sensors and take it in the variables? For example: variables for ID 1 could be name1, date1, time1 and value1

Shamas S
  • 7,507
  • 10
  • 46
  • 58
Martin
  • 93
  • 7

3 Answers3

0

First if you don't have particular need to use the format specified above(i.e. Name_of_sensor;02/26/2015;21:16;22.88), you should consider using JSON. JSON is very popular data exchange format specially in Server-Client case.

Your JSON should look something like:

{
    "sensors": [
        {
            "id": 1,
            "date": "02/01/2015",
            "temp": 12.5
        }
    ]
}

JSON Tutorial

JSON with iOS Objective-C Tutorial

Otherwise you can separate components on ','. See the answer here

Community
  • 1
  • 1
Taha Samad
  • 1,135
  • 1
  • 9
  • 22
0

As Taha says you should look at JSON however if you require the structure you are using for your API then you should split the string based on the ;

Assuming your API returns a string that you store as an NSString called string_from_api

NSArray *dataArray = [string_from_api componentsSeparatedByString: @";"];
NSString *firstID = [dataArray objectAtIndex: 0];

You could also use a for loop to get all of the data out and process it

NSArray *dataArray = [string_from_api componentsSeparatedByString: @";"];

for(int i = 0; i < dataArray.count; i++)
{
    NSString *thisID = [dataArray objectAtIndex: 0];
    //Then call the function you have created that gets the temperature, passing the ID
    [self function_to_get_temperature:firstID];
}
Adam Richardson
  • 2,518
  • 1
  • 27
  • 31
0

If you have the result in this format only i,e

Name_of_sensor;02/26/2015;21:16;22.88

and the last part always will have the temperature, then write this

NSString *fromAPI = @"Name_of_sensor;02/26/2015;21:16;22.88";
NSArray *breakingString = [fromAPI componentsSeparatedByString:@";"];    
NSString *temperature = [breakingString objectAtIndex:[breakingString count]-1];
NSLog(@"%@",temperature);

Hope this helps

Saheb Roy
  • 5,899
  • 3
  • 23
  • 35
  • Can you say me please, how to get value of *fromAPI from my API using this string: http://someurl.com/api/actual/1 ? – Martin Feb 27 '15 at 15:59
  • well that depends on how the structure of the JSON/XML is from your webservice that you are getting, Cast it into a dictionary and traverse the dictionary with the appropriate key and u will get this value. – Saheb Roy Mar 03 '15 at 10:13