gpt4 book ai didi

ios - NSString 工作一次然后不工作

转载 作者:行者123 更新时间:2023-11-28 19:17:40 24 4
gpt4 key购买 nike

这是我第一次使用这个网站,我对 Objective-c 还很陌生。我确定这是一个简单的问题,但出于某种原因我遇到了很多问题。该应用程序旨在让用户通过文本字段输入一个字符串,然后它会选择句子的其余部分并显示出来。问题似乎是我的 *name 将在键盘方法之后保留并在 changelabel 方法中工作一次。然后,如果我再次按下按钮,调用 changelabel 方法,名称似乎已被释放并使应用程序崩溃。 #进口 #import "数组.h"

@interface RandomBoredViewController : UIViewController {
UILabel *label;
UIButton *button;
UITextField *textField;
Array *array;
NSString *name;
NSString *description;
NSMutableString *whole;
}
@property (nonatomic, retain) IBOutlet UILabel *label;
@property (nonatomic, retain) IBOutlet UIButton *button;
@property (nonatomic, retain) IBOutlet UITextField *textField;
@property (nonatomic, retain) Array *array;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *description;
@property (nonatomic, retain) NSMutableString *whole;

-(IBAction) keyBoard;
-(IBAction) changeLabel;

@end

和我的.m

#import "RandomBoredViewController.h"

@implementation RandomBoredViewController

@synthesize label;
@synthesize checker;
@synthesize button;
@synthesize textField;
@synthesize array;
@synthesize name;
@synthesize description;
@synthesize whole;


-(IBAction) changeLabel {
NSLog(@"Button being pushed");
description = [array getString];
NSLog(@"%@",description);
NSLog(@"%@",name);
name = [NSString stringWithString:name];
whole = [NSMutableString stringWithString:name];
NSLog(@"%@",name);
NSLog(@"%@",whole);
[whole appendString:description];
NSLog(@"%@",name);
NSLog(@"%@",whole);
label.text = whole;
NSLog(@"%@",name);
}

-(IBAction) keyBoard {
name = [NSString stringWithString:textField.text];
NSLog(@"%@",name);
label.text = [NSString stringWithString: name];
[textField resignFirstResponder];
}

- (void)viewDidLoad {
[super viewDidLoad];
array = [[Array alloc]init];
[array createArray];
NSLog(@"%i",[array arrayCount]);
whole = [[NSMutableString alloc]init];
name = [[NSString alloc]init];
}

- (void)dealloc {
[super dealloc];
[label release];
[button release];
[textField release];
[array release];
//[name release];
[description release];
}

@end

最佳答案

您正在将名称设置为 NSString 的自动释放实例,这可能是导致您的应用程序崩溃的原因。

使用

self.name = [NSString stringWithString:textField.text];

你的合成修改器将保留 NSString 并防止它被释放。

关于ios - NSString 工作一次然后不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11194884/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com