解析XML代码问题
我在该网站的一篇文章中发现了以下有关在 Objective-C 中解析 XML 的技巧。然而,y
似乎总是等于 null。我想知道是否有人可以澄清这里发生了什么。
NSString * q = [myURL query];
NSArray * pairs = [q componentsSeparatedByString:@"&"];
NSMutableDictionary * kvPairs = [NSMutableDictionary dictionary];
for (NSString * pair in pairs) {
NSArray * bits = [pair componentsSeparatedByString:@"="];
NSString * key = [[bits objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSString * value = [[bits objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
[kvPairs setObject:value forKey:key];
}
NSLog(@"y = %@", [kvPairs objectForKey:@"y"]);
I found the following tip for parsing XML in Objective-C on a post on this site. However, y
always seems to equal null. I'm wondering if someone can clarify what is going on here.
NSString * q = [myURL query];
NSArray * pairs = [q componentsSeparatedByString:@"&"];
NSMutableDictionary * kvPairs = [NSMutableDictionary dictionary];
for (NSString * pair in pairs) {
NSArray * bits = [pair componentsSeparatedByString:@"="];
NSString * key = [[bits objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSString * value = [[bits objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
[kvPairs setObject:value forKey:key];
}
NSLog(@"y = %@", [kvPairs objectForKey:@"y"]);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
建立一个由 URL 调用组成的字符串,例如 http://www .somewhere.com/form?name=john&x=123&y=456
NSString * q = [myURL 查询];
创建键值对数组 name=john, x=123, y=456
NSArray *pairs = [q elementsSeparatedByString:@"&"];
初始化一个字典,用于存储键值对
NSMutableDictionary * kvPairs = [NSMutableDictionary 字典];
初始化临时变量“pair”并迭代数组
for (NSString *pair inpair) {
将键、= 的左侧与值分开
NSArray * 位 = [pair elementsSeparatedByString:@"="];
存储密钥
NSString * key = [[bits objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
存储值
NSString * value = [[bits objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
用键的值填充字典
[kvPairs setObject:key 的值:key];
在这里
您将打印出键“y”的值,该值应该是 456
NSLog(@"y = %@", [kvPairs objectForKey:@"y"]);
如果没有其余的代码,也许发送的 url 没有键名称“y”,就像我在示例中分解的那样。
我希望这有帮助!
Establish a string made from a URL call something like http://www.somewhere.com/form?name=john&x=123&y=456
NSString * q = [myURL query];
Create an array of key value pairs name=john, x=123, y=456
NSArray * pairs = [q componentsSeparatedByString:@"&"];
initialize a dictionary for storing key-value pairs
NSMutableDictionary * kvPairs = [NSMutableDictionary dictionary];
Initialize a temp variable "pair" and iterate through the array
for (NSString * pair in pairs) {
Separate the key, left side of = from the value
NSArray * bits = [pair componentsSeparatedByString:@"="];
store the key
NSString * key = [[bits objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
store the value
NSString * value = [[bits objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
fill the dictionary with values for the keys
[kvPairs setObject:value forKey:key];
}
Here you will print out the value for the key "y" which should be 456
NSLog(@"y = %@", [kvPairs objectForKey:@"y"]);
Not having the rest of your code, perhaps the url being sent does not have a key name "y" like I've broken down in my example.
I hope this helps!!