如何在多个实例方法中访问指针?

发布于 2024-11-28 03:22:17 字数 954 浏览 1 评论 0 原文

我不明白如何声明一个可以在多个方法中访问的指针。以下代码使用 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!

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

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

发布评论

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

评论(2

风启觞 2024-12-05 03:22:17

处理此问题的正确方法是处理“myContainer”字段的内存管理。

我会选择:

myContainer = [sender.titleLabel.text copy];

建议对 NSString 使用副本。该讨论的相关主题位于此处

另外,请不要忘记释放“myContainer”字段的内存。您可以在 dealloc 方法中执行此操作:

-(void) dealloc {
    [myContainer release];
    [super dealloc];    
}

The proper way of handling this would be to take care of memory management for your 'myContainer' field.

I would go with:

myContainer = [sender.titleLabel.text copy];

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:

-(void) dealloc {
    [myContainer release];
    [super dealloc];    
}
淡忘如思 2024-12-05 03:22:17

您说这是一个内存管理问题是正确的。基本上,您并不是“声明所有权”存储在 myContainer 中的字符串。在调用 storeToMyContainer: 后的一段(短)时间,包含发送者字符串的自动释放池被耗尽,并且因为您没有说您仍在使用它,所以它不再存在。

要声明该字符串的所有权,您必须保留(或复制)它。因此,更新您的代码:

- (IBAction)storeToMyContainer:(UIButton *)sender {
    myContainer = [sender.titleLabel.text retain];
}

另一种方法是使用 内存管理编程指南,特别是基本内存管理规则

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 after storeToMyContainer: 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 (or copy) it. So, updating your code:

- (IBAction)storeToMyContainer:(UIButton *)sender {
    myContainer = [sender.titleLabel.text retain];
}

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.

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