解析XML代码问题

发布于 2024-09-26 13:19:45 字数 692 浏览 4 评论 0原文

我在该网站的一篇文章中发现了以下有关在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪操控生活 2024-10-03 13:19:45

建立一个由 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!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文