我不明白如何声明一个可以在多个方法中访问的指针。以下代码使用 myContainer
来存储调用 useMyContainer
时使用的数字。
@interface MyViewController : UIViewController {
NSString *myContainer;
IBOutlet UILabel *display;
}
- (IBAction)storeToMyContainer: (UIButton *)sender;
- (IBAction)useMyContainer: (UIButton *)sender;
@end
@implementation MyViewController
- (IBAction)storeToMyContainer: (UIButton *)sender {
myContainer = sender.titleLabel.text;
}
- (IBAction)useMyContainer: (UIButton *)sender {
[someOtherClass doSomethingWith:[myContainer doubleValue]];
}
@end
我不明白的是,当我以相同的方式使用 display
时,我没有问题。我需要做什么才能以这种方式访问 useMyContainer
中的 myContainer
?
一些想法:我知道这是一个内存管理问题,并且我几乎确定在显示时调用了 retain
(可能是通过 .xib 文件?),这就是为什么 display< /code> 停留的时间足够长,可以在两种方法中使用。
我知道一个解决方法涉及使用 double 和 int,但我认为这很混乱,并且当我正在上这门课时,我想知道处理这个问题的 Baller 方法。
感谢您的帮助!
I do not understand how to declare an pointer that can be accessed in more than one method. The following code uses myContainer
to store a number which is utilized when useMyContainer
is called.
@interface MyViewController : UIViewController {
NSString *myContainer;
IBOutlet UILabel *display;
}
- (IBAction)storeToMyContainer: (UIButton *)sender;
- (IBAction)useMyContainer: (UIButton *)sender;
@end
@implementation MyViewController
- (IBAction)storeToMyContainer: (UIButton *)sender {
myContainer = sender.titleLabel.text;
}
- (IBAction)useMyContainer: (UIButton *)sender {
[someOtherClass doSomethingWith:[myContainer doubleValue]];
}
@end
What I don't understand is that when I use display
in the same manner, I don't have a problem. What do I need to do to access myContainer
in useMyContainer
in this manner?
Some thoughts: I know this is a memory management issue, and I am almost sure that a retain
is being called on display (probably by the .xib file?) and that is why display
hangs around long enough to be used in both methods.
I know a work around that involves using a double and an int, but I consider this to be messy, and as I'm taking a class on this I wanna to know the baller way to handle this.
Thanks for your help!
发布评论
评论(2)
处理此问题的正确方法是处理“myContainer”字段的内存管理。
我会选择:
建议对 NSString 使用副本。该讨论的相关主题位于此处。
另外,请不要忘记释放“myContainer”字段的内存。您可以在 dealloc 方法中执行此操作:
The proper way of handling this would be to take care of memory management for your 'myContainer' field.
I would go with:
It is recommended to use copy for NSString. Relevant thread for that discussion is here.
Also, please don't forget to release the memory for the 'myContainer' field. You can do that in your dealloc method:
您说这是一个内存管理问题是正确的。基本上,您并不是“声明所有权”存储在
myContainer
中的字符串。在调用storeToMyContainer:
后的一段(短)时间,包含发送者字符串的自动释放池被耗尽,并且因为您没有说您仍在使用它,所以它不再存在。要声明该字符串的所有权,您必须
保留
(或复制
)它。因此,更新您的代码:另一种方法是使用 内存管理编程指南,特别是基本内存管理规则。
You are correct in saying that this is a memory management issue. Basically, you're not "claiming ownership" of the string being stored in
myContainer
. Some (short) period of time afterstoreToMyContainer:
is called, the autorelease pool containing the sender's string is drained and, because you didn't say that you're still using it, it ceases to exist.To claim ownership of the string, you must
retain
(orcopy
) it. So, updating your code:Another approach would be to use properties. Either way, I recommend you have a read over the Memory Management Programming Guide, specifically Basic Memory Management Rules.