Question

Here is the XML code seperated by commas, i am parsing this as a string, now need to convert as integer: struct in .h file: NSString * positions;

XML file: 100,280,924,320

here is the code:

 NSArray * positons = [partyMember elementsForName:@"position"];
    if (positons.count > 0) {
        GDataXMLElement *firstName = (GDataXMLElement *) [positons objectAtIndex:0];
        //parsed[i].positons = firstName.stringValue;
        parsed[i].positons = firstName.stringValue;
        NSLog(@"position-%@,\n",parsed[i].positons);

first name is locally declared , parsed[i].positions has da parsed value,

Was it helpful?

Solution 4

here is what i did :) change Objectatindex value while applying it in viewcontroller

 NSArray * positons = [partyMember elementsForName:@"position"];
    if (positons.count > 0) {
        GDataXMLElement *firstName = (GDataXMLElement *) [positons objectAtIndex:0];
        //parsed[i].positons = firstName.stringValue;
        parsed[i].positons = firstName.stringValue;
        NSLog(@" parsed position is %d, i = %d\n",[parsed[i].positons integerValue],i);
        NSArray* arr = [parsed[i].positons componentsSeparatedByString:@","];
       // firstStr = [[arr objectAtIndex:0] intValue];
        NSLog(@"position-%d,\n",[[arr objectAtIndex:2] intValue]);

OTHER TIPS

You can get an array of strings using:

NSArray* arr = [fullStr componentsSeparatedByString:@","] 

and then get an int from that

int firstInt = [[arr objectAtIndex:0] intValue]; 

You can use following code for getting the integer values from string:

  1. First get the comma separated objects in any array like this:

    NSArray *arrObjects=[parsed[i].positons componentsSeparatedByString:@","]; 
    
  2. Then by using that array convert string value to integer as below:

    int iFirstValue=[[arrObjects objectAtIndex:0] intValue];
    

similar for others.

You can change a NSString to int value by:

[string intValue];
[string floatValue];

but this make sense only if the the value is number but in string form,

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top