为什么 NSString 变空(没有原因)?

发布于 2024-11-30 17:11:32 字数 969 浏览 2 评论 0原文

我对 Objective C 很着迷。请看下面的代码。

FirstViewController.h:

@interface FirstViewController : UIViewController {
    IBOutlet UITextView *textView;
    IBOutlet UIBarButtonItem *button;
    NSString* superString;
}

- (IBAction)buttonDown;

FirstViewController.m:

- (void)viewDidLoad {
    [super viewDidLoad];
    superString = [NSString stringWithFormat:"%@", @"A_super_string_file.txt"];
    [self buttonDown];
}

- (IBAction)buttonDown {
    NSError* err = nil;
    NSString* text = [NSString stringWithContentsOfFile:superString encoding:NSUTF8StringEncoding error:&err];
    [textView setText:text];
}
  1. 运行应用程序。 A_super_string_file.txt 成功显示在textView中。
  2. 按下按钮。应用程序崩溃是因为 superString 引用了与步骤 1 中相同的地址,但该地址上有另一个变量或没有任何变量。 superString 上的打印说明不显示任何内容或随机其他变量(甚至可能是不同类型)。

会发生什么?我是一名技能开发人员,但对这种情况一无所知。非常感谢您的帮助!

更新:可能,它在工作中隐藏了自动释放。但是什么时候释放内存呢?每次都在不同的时刻还是在某个特定的时刻?

I am going crazy with Objective C. Please look to the following code.

FirstViewController.h:

@interface FirstViewController : UIViewController {
    IBOutlet UITextView *textView;
    IBOutlet UIBarButtonItem *button;
    NSString* superString;
}

- (IBAction)buttonDown;

FirstViewController.m:

- (void)viewDidLoad {
    [super viewDidLoad];
    superString = [NSString stringWithFormat:"%@", @"A_super_string_file.txt"];
    [self buttonDown];
}

- (IBAction)buttonDown {
    NSError* err = nil;
    NSString* text = [NSString stringWithContentsOfFile:superString encoding:NSUTF8StringEncoding error:&err];
    [textView setText:text];
}
  1. Run the application. A_super_string_file.txt successfully displayed in textView.
  2. Press the button. The application crashes because superString refers on the same address as in the step 1 but there is another variable or nothing on this address. Print Description on superString displays nothing or random other variable (may be even different type).

What happens? I am a skill developer but have no idea in this case. Thanks a lot for help!

UPDATE: Possibly, it's hidden autorelease in the work. But when it releases memory? On different moment every time or on some exact moment?

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

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

发布评论

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

评论(2

我们只是彼此的过ke 2024-12-07 17:11:32

NSString 的 +stringWithFormat: 是自动释放的。如果您希望它保留下来,您需要保留它。

属性是实现此目的的好方法,因为它减少了您需要担心的内存管理量。

.h.m

@interface FirstViewController : UIViewController {
    IBOutlet UITextView *textView;
    IBOutlet UIBarButtonItem *button;
    NSString* superString;
}

@property (nonatomic, copy) NSString* superString;

- (IBAction)buttonDown;

@synthesize superString

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setSuperString:[NSString stringWithFormat:"%@", @"A_super_string_file.txt"]];
    [self buttonDown];
}

- (IBAction)buttonDown {
    NSError* err = nil;
    NSString* text = [NSString stringWithContentsOfFile:superString encoding:NSUTF8StringEncoding error:&err];
    [textView setText:text];
}

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

NSString's +stringWithFormat: is autoreleasing. You need to retain it if you want it to stay around.

Properties are a good way to do this, because it cuts down how much memory management you need to worry about yourself.

.h

@interface FirstViewController : UIViewController {
    IBOutlet UITextView *textView;
    IBOutlet UIBarButtonItem *button;
    NSString* superString;
}

@property (nonatomic, copy) NSString* superString;

- (IBAction)buttonDown;

.m

@synthesize superString

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setSuperString:[NSString stringWithFormat:"%@", @"A_super_string_file.txt"]];
    [self buttonDown];
}

- (IBAction)buttonDown {
    NSError* err = nil;
    NSString* text = [NSString stringWithContentsOfFile:superString encoding:NSUTF8StringEncoding error:&err];
    [textView setText:text];
}

- (void)dealloc {
    [superString release];
    [super dealloc];
}
疯狂的代价 2024-12-07 17:11:32

您需要保留 superString。

[superString retain];

创建后立即。

然后在dealloc中释放它。

You need to retain superString.

[superString retain];

immediately after creating it.

and then release it in dealloc.

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