与存储在 NSMutableArray 中的 UIView 进行交互
大菜鸟需要帮助理解事物。
NSMutableArray 中存储了三个 UIView
lanes = [[NSMutableArray arrayWithCapacity:3] retain];
- (void)registerLane:(Lane*)lane {
NSLog (@"registering lane:%i",lane);
[lanes addObject:lane];
}
我在 NSLog 中的
: 注册通道:89183264 NSLog 中显示的值(89183264)就是我所追求的。 我希望能够将该数字保存在变量中,以便能够在代码中的其他地方重用它。
我能想到的最接近的是:
NSString *lane0 = [lanes objectAtIndex:0];
NSString *description0 = [lane0 description];
NSLog (@"description0:%@",description0);
问题是description0获取整个UIView对象,而不仅仅是单个数字(dec 89183264是十六进制0x550d420)
description0的内容:
description0:<Lane: 0x550d420; frame = (127 0; 66 460); alpha = 0.5; opaque = NO; autoresize = RM+BM; tag = 2; layer = <CALayer: 0x550d350>>
我不明白的是为什么我得到正确的十进制值与 NSLog 很容易,但似乎无法以任何其他方式将其从 NSMutableArray 中取出。我确信我在这里缺少一些“基本知识”,如果有人能花时间解释这里发生的事情,我将不胜感激,以便我最终可以继续前进。这是漫长的一天学习。
为什么我不能用以下内容轻松保存 89183264 号码:
NSInteger * mylane = lane.id;
or
NSInteger * mylane = lane;
谢谢大家
a big noob needs help understanding things.
I have three UIViews stored inside a NSMutableArray
lanes = [[NSMutableArray arrayWithCapacity:3] retain];
- (void)registerLane:(Lane*)lane {
NSLog (@"registering lane:%i",lane);
[lanes addObject:lane];
}
in the NSLog I see: registering lane:89183264
The value displayed in the NSLog (89183264) is what I am after.
I'd like to be able to save that number in a variable to be able to reuse it elsewhere in the code.
The closest I could come up with was this:
NSString *lane0 = [lanes objectAtIndex:0];
NSString *description0 = [lane0 description];
NSLog (@"description0:%@",description0);
The problem is that description0 gets the whole UIView object, not just the single number (dec 89183264 is hex 0x550d420)
description0's content:
description0:<Lane: 0x550d420; frame = (127 0; 66 460); alpha = 0.5; opaque = NO; autoresize = RM+BM; tag = 2; layer = <CALayer: 0x550d350>>
what I don't get is why I get the correct decimal value with with NSLog so easily, but seem to be unable to get it out of the NSMutableArray any other way. I am sure I am missing some "basic knowledge" here, and I would appreciate if someone could take the time and explain what's going on here so I can finally move on. it's been a long day studying.
why can't I save the 89183264 number easily with something like:
NSInteger * mylane = lane.id;
or
NSInteger * mylane = lane;
thank you all
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我真的很困惑为什么要保存视图的内存位置?因为这就是您的“89183264”号码。这是指针的位置。当您打电话时:
...您是否收到实际打印的内容?打印的数字是什么意思?
这似乎是一个非常糟糕的主意,特别是当你继承 UIView 时,你已经有了一个可爱的 .tag 属性,你可以分配你选择的 int 。
你让生活变得比它需要的更加复杂。只需使用指针即可。假设我有一个包含大量 UIView 的数组:
这就是您想要做的事情 - 它比较两个指针 - 并且不需要任何整数等的处理。
I'm really confused as to why you want to save the memory location of the view? Because that's what your '89183264' number is. It's the location of the pointer. When you are calling:
...do you get what's actually being printed out there? What the number that's being printed means?
It seems like a really bad idea, especially when if you're subclassing UIView you've already got a lovely .tag property which you can assign an int of your choosing.
You're making life infinitely more complex than it needs to be. Just use a pointer. Say I have an array containing lots of UIViews:
That does what you're trying to do - it compares two pointers - and doesn't need any faffing around with ints, etc.