SOAP 应用程序变量 - NSNumber 或 NSString 表示整数?

发布于 2024-08-29 07:04:37 字数 321 浏览 5 评论 0原文

在 SOAP 应用程序中,与服务器的所有通信都是文本,当服务器需要整数时,发送的是文本值,返回的整数则作为文本发回。

要让应用程序处理整数,必须将返回的字符串转换为数字,例如 NSInteger,然后将它们保存到数组中,它们必须是对象(NSNumber),但每当需要数值时,您都必须提取它因此,最好将所有基于数字的变量保留为 NSStrings &仅在需要时提取数值?

基本上我的问题是,在肥皂应用程序中,将所有变量都作为 NSString 不是更简单吗?因为它们在用法上与 NSNumber 对象类似,并且将所有数字保留为字符串至少可以节省转换回来的时间。每当涉及服务器通信时都会出现。

In a SOAP app, all communication with the server is as text, when the server needs an integer it is the text value that gets sent and returned integers are sent back as text.

To have the app working with integers one has to convert the returned strings into numbers e.g. NSInteger but then to, for example, save them to an array they must be objects (NSNumber) but then whenever the numeric value is needed you have to extract it so is it better to leave all number based variables as NSStrings & only extract the numeric value if needed?

Basically my question is, in a soap app, isn't it simpler to just have all variables as NSStrings? Since they are similar to NSNumber objects in terms of usage and leaving all numbers as strings at least saves having to convert back & forth whenever there is server communication involved.

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

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

发布评论

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

评论(1

自在安然 2024-09-05 07:04:37

NSString 相比,在 NSNumber 中存储整数的开销可能会更低——无论看起来多么小。根据字符编码,字符串每个字形可以是多个字节,然后您需要在字符串末尾有一个空字符来终止它。

解析器必须完成所有这些工作,以便在一天结束时将字符串重新转换为整数,例如当您不可避免地需要对这些值进行算术时。

此外,使用 NSNumber 将使您更清楚地了解 SOAP 请求或响应中正在使用的原始数据类型,不仅是在您编写应用程序时,而且是在您几个月后返回应用程序时。该行,您需要进行更改。

Storage of an integer in an NSNumber will likely have lower overhead — however minimal it might seem — compared with an NSString. A character string can be multiple bytes per glyph depending on the character encoding, and then you need a null character at the end of the string to terminate it.

A parser has to work through all of that to turn a string back into an integer at the end of the day, such as when you will inevitably need to do arithmetic on these values.

Further, using NSNumber will make it clearer to you what primitive data type you are working with from the SOAP request or response, not only when you write your app, but when you come back to it several months down the line and you need to make changes.

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