XMLParsing - 将字符串转换为NSNumber

时间:2012-01-12 08:19:34

标签: objective-c ios xcode

有人可以告诉我我需要做什么来解决这个问题

  1. 我有一个对象UserPrefs * userpref
  2. 有一个名为userpref.userid的变量,它是一个NSNumber
  3. 我正在解析XML,这里的一切都很好,直到我将“myxmluseridnumber”元素分配给userpref.userid
  4. 这是错误消息。

    将'NSMutableString * _strong'传递给'NSNumber

    类型参数的指针类型不兼容
        - (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
    
            if ([elementName isEqualToString:@"User"]) {
                inItemElement = YES;
            }
    
            if (inItemElement && [elementName isEqualToString:@"myxmluseridnumber"]) {
                capturedCharacters = [[NSMutableString alloc] initWithCapacity:100];
            }
        }
    
        - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
           if(capturedCharacters != nil) {
               [capturedCharacters appendString:string];
           }
        }
    
        - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    
            if (inItemElement && [elementName isEqualToString:@"myxmluseridnumber"]) {
                NSLog(@"%@ - myxmlnumber", capturedCharacters);
                //This is what is producing my error
                userpref.userid = capturedCharacters;
                capturedCharacters = nil;
            }
    
            if ([elementName isEqualToString:@"User"]) {
                inItemElement = NO;
            }
    
        }
    

    由于

2 个答案:

答案 0 :(得分:1)

由于用户ID是NSNumber,您应该从字符串NSNumber

创建capturedCharacters
  

userpref.userid = [NSNumber numberWithInt:[capturedCharacters   的intValue]];

答案 1 :(得分:0)

用以下代码替换错误产生线:

userpref.userid = [capturedCharacters intValue];

(假设您的capturedCharacters是s NSString