简体   繁体   English

如何在Objective C中将基于XML的String值转换为整数

[英]How to convert XML based String value to integer in Objective C

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代码,我将其解析为字符串,现在需要转换为整数:.h文件中的struct:NSString * positions;

XML file: 100,280,924,320 XML文件: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, 第一个名称是本地声明的,解析[i] .positions具有da解析值,

You can get an array of strings using: 您可以使用以下命令获取字符串数组:

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

and then get an int from that 然后从中获取一个int

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: 您可以通过以下方式将NSString更改为int值:

[string intValue];
[string floatValue];

but this make sense only if the the value is number but in string form, 但只有当值是数字但是以字符串形式时才有意义

here is what i did :) change Objectatindex value while applying it in viewcontroller 这是我做的:)在viewcontroller中应用它时更改Objectatindex值

 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]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM