我已经在 ViewdidLoad 中用一些字符串值声明了 NSString,比如..
int i=1;
strval=[NSString stringWithFormat:@"%03d",i];
strval=[NSString stringWithFormat:@"S%@",strval];
NSLog(@"Value %@",strval);
它给出了 S001 的正确结果,但是当我在 IBAction 中打印相同的结果时,
- (IBAction)stringvalue:(id)sender {
NSLog(@"Value %@",strval);
}
它每次都给出未知值。有时它会抛出 EXEC_BAD_ACCESS 错误。
请帮帮我..
最佳答案
尝试这样的事情
在.h 中
@property (nonatomic, strong) NSString *strval;
.m
@synthesize strval = _strval
- (void)viewDidLoad
{
int i = 4;
// ARC
_strval = [NSString stringWithFormat:@"hello %d", i];
// None ARC
// strcal = [[NSString alloc] initwithFormat:@"hello %d",i];
NSLog(@"%@", _strval);
// Prints "hello 4" in console (TESTED)
}
- (IBAction)buttonPress:(id)sender
{
NSLog(@"%@", _strval);
// Prints "hello 4" in console (TESTED)
}
使用 ARC。这已经过测试,并且按照问题提出的方式工作。
关于iphone - IBAction 中格式错误的 NSstring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13234264/