使用 Objective-C 分割输入字符串

发布于 2024-12-28 15:42:26 字数 256 浏览 2 评论 0原文

我在 iPhone 项目中使用 ASIHTTPREQUEST 来获取一些看起来像这样的数据(作为 NSString):

name:daniel, age:22, occupation:runner, name:greg, age:32, occupation:plumber

并且我想将它们解析为字典或数组,以便我可以获得所有名称或所有职业等。

我该怎么办这在 Objective-C 中吗?

谢谢

I am using ASIHTTPREQUEST in an iPhone project to get some data that looks something like this (as an NSString):

name:daniel, age:22, occupation:runner, name:greg, age:32, occupation:plumber

and I want to parse these into dictionaries or arrays so that I can get all names, or all occupations etc.

How can I do this in objective-c?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

尽揽少女心 2025-01-04 15:42:26

一种方法是使用 NSString 的 -componentsSeparatedByString: 方法以及分隔符字符串 @", " 将字符串分解为单独的子字符串,每个子字符串对应一个键/值对。然后,您可以再次使用它与分隔符 @":" 将每个子字符串分解为键和值,并将它们插入字典中。

另一种方法是使用 NSScanner 扫描输入字符串,按顺序获取键和值。

One way is to use NSString's -componentsSeparatedByString: method with a separator string of @", " to break the string up into separate substrings, one for each key/value pair. Then you can use it again with a separator of @":" to break each substring into a key and a value, and insert these into a dictionary.

Another way is to use NSScanner to scan through your input string, taking the keys and values sequentially.

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