gpt4 book ai didi

objective-c - XMLParsing - 将字符串转换为 NSNumber

转载 作者:行者123 更新时间:2023-11-29 04:51:03 25 4
gpt4 key购买 nike

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

  1. 我有一个对象 UserPrefs *userpref
  2. 有一个变量叫userpref.userid,它是一个NSNumber
  3. 我正在解析 XML,这里的一切都运行良好,直到我将“myxmluseridnumber”元素分配给 userpref.userid

这是错误消息。

将“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;
}

}

谢谢

最佳答案

由于 userid 是一个 NSNumber,因此您应该从字符串 capturedCharacters 创建一个 NSNumber

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

关于objective-c - XMLParsing - 将字符串转换为 NSNumber,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8831906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com