如何确定iOS(id)对象的结构?

发布于 2024-12-27 16:37:44 字数 345 浏览 5 评论 0原文

当我从 iOS 应用程序调用某个函数时,它会返回 id 数据类型。我无法查看这个函数,所以我不知道它在做什么或返回类型到底是什么。

如果我使用 NSLog("@"...) 将其打印到控制台,我会得到类似于以下内容的字符串: 2012-01-18 19:03:08.915 HelloWorld[165:707]按键状态

有什么方法可以让我确定这个基本 Id 对象的结构吗?我将如何获取该响应的特定部分,例如“按键状态”。字符串解析似乎是一个可怕的想法,但也许这是唯一的方法。也许数据真的只是一个 NSString

谢谢!

When I call a certain function from my iOS app, it returns an id data type. I can't see into this function so I don't know what it's doing or what the return type really is.

If I print it to console using NSLog("@"...) I get a string similar to this:
2012-01-18 19:03:08.915 HelloWorld[165:707] Key Press State.

Is there any way for me to determine the structure of this basic Id object? How would I go about getting a specific part of that response out, such as "Key press state". String parsing seems like a horrible idea, but maybe that's the only way. Perhaps the data really is just an NSString?

Thanks!

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

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

发布评论

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

评论(2

﹎☆浅夏丿初晴 2025-01-03 16:37:44

试试这个:

NSLog(@"Mystery object is a %@", NSStringFromClass([mysteryObject class]));

Try this:

NSLog(@"Mystery object is a %@", NSStringFromClass([mysteryObject class]));
橘和柠 2025-01-03 16:37:44

如果您查看,您将看到用于查询对象的类、方法选择器、ivars 等的方法。但是,您通常不想这样做,因为它破坏了封装并可能导致您依赖将来可能会更改的实现细节,因此请小心。您可以在此处阅读更多信息。

If you look in <objc/runtime.h> you'll see methods for querying an object about its class, method selectors, ivars, etc. However, you don't usually want to do that, as it breaks encapsulation and can lead to you relying on implementation details that might change in the future, so be careful with it. You can read more here.

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