2011-08-31 60 views
0

解决....在检查身体的底部..当我追加一个NSString与另一个NSstring它给我一个错误“EXC BAD ACCESS”?

我使用XCODE 4

我有一个货币计算器应用程序中,我必须按数字键,我要看到它的为textLabel。

格式,如:“$ 500”

因此,对于$我追加吧..

的方法效果很好两次,但第三次崩溃..

休息,如果你看到代码你会有一个想法。

在我的应用我有三个字符串变量...

- (无效)viewDidLoad中 {

current =[[NSString alloc] retain]; 

    current=[[NSUserDefaults standardUserDefaults] valueForKey:@"Total"]]; 



    NSString *newCurrent=[NSString stringWithFormat:@"$ %@",current]; 

    textViewer.text=newCurrent; 

NSLog(@"%@",current); 

}

- (IBAction为)buttonDigitPressed:(ID)发送{

NSString *str= (NSString*)[sender currentTitle]; 

//NSLog(@"1 The Value Of String %@",str); 

NSLog(@"Befor Appending %@",current); 


if([current isEqualToString:@"0"]) 
{ 
    current = str; 
} 
else 
{ 

    current = [current stringByAppendingString:str]; 

    NSLog(@"After Appending %@",current); 

} 


NSString *[email protected]"$ "; 

newCurrent = [newCurrent stringByAppendingString:current]; 

textViewer.text= newCurrent; 

} 错误是第一次工作荷兰国际集团罚款,但

当我把它称为第二次,目前是指向某些标准时间 格式化的值,有些时候它指出了一些文件名值,

错误是:EXC错误访问
当我这样做没有newCurrent意味着没有附加一个字母它工作正常。

-------解决---------------- 每一件事情是确定我的代码我只是要正确[当前保留];在哪里我将字符串追加到当前。

当我收到错误EXC BAD ACCESS意味着我指的是以前发布的对象。 因此,我保留了对象。

+1

尝试nslogging所有的变量时,你打它第三次。让我们知道当前和当前标题在崩溃时的价值。 – mayuur

+0

其实先生,我做了NSLogs,在方法开始时第一次的值是在追加“96”之前,追加了“961”(因为我在UI中按了1次,第二次当该方法在追加前称为当前值实际上,每当它遇到当前在这个时候exc坏访问..所以我不能看到nslog第二次...,希望你的一些解决方案你建议.. –

回答

0

试试这个代码(没有内存泄漏):

-(void) viewDidLoad 
{ 
    current=[[NSString alloc] initWithString:@"20"]; 
} 

-(IBAction)buttonDigitPressed:(id)sender 
{ 
    NSString *str= (NSString*)[sender currentTitle]; 

    NSString *nCurrent = [current stringByAppendingString:str]; 
    [current release]; 
    current = [nCurrent retain]; 

    NSString *newCurrent=[[NSString alloc] initWithFormat:@"a%@", current]; 
    textViewer.text= newCurrent; 
    [newCurrent release]; 
} 

- (void) dealloc 
{ 
    [current release]; 
    [super dealloc]; 
} 
+0

这只是要反复泄漏内存 –

+0

在这个问题内存管理是不是目标,在消除目前的问题后,他可以做内存管理,我们只看到部分代码... – Nekto

+0

更新了我的答案,现在没有内存泄漏... – Nekto

0

在这两种情况下都不需要分配或保留字符串。

in viewdidload
s = [[NSString alloc] init];
s = @“a”;

按钮点击

NSString *newStr = [NSString stringWithFormat:@"b"]; 
s = [s stringByAppendingString:newStr]; 
NSLog(@"s is :%@",s); 
0

你好Arpit,

您可以在声明.h文件中的NSString的性质和合成它在.m文件,所以我觉得它的帮助你。

。.h文件

@property (nonatomic,retain) 
NSString *current; 

.m文件

@synthesize current; 
0

Theres很多的问题在这里。

  1. 当您将current =设置为新分配的对象时,您的viewDidLoad中存在内存泄漏,则将其替换为@“20”;你现在已经失去了指向分配内存的指针。正确的做法是简单地删除alloc行。
  2. 当你设置newCurrent = [newCurrent stringByAppendingString:current]时,你有另一个内存泄漏;原因与1相同。

您或者需要更好地管理您的内存,或者只是使用NSMutableStrings。改变当前是一个NSMutableString的alloc/init它在你的init或viewDidLoad,然后只使用[current appendString:str];

基于你粘贴的代码我会做下面的事情。

在@interface

声明属性

@property (nonatomic, retain) NSString *current; 

在@implementation @synthesize它

@synthesize current; 

- (void) viewdidload 
{ 
    self.current = @"20"; 
} 

-(IBAction)buttonDigitPressed:(id)sender 
{ 
    NSString *str= (NSString*)[sender currentTitle]; 
    self.current = [self.current stringByAppendingString:str]; 

    NSString *newCurrent = @"a"; 
    newCurrent = [newCurrent stringByAppendingString:self.current]; 

    textViewer.text = newCurrent; 
} 
+0

Sooryyn尽管删除了alloc保留,它仍然无效.. –

+0

不工作如何?只是将保留添加到您的代码并没有解决它。 –

+0

这是完成谢谢....由reatin .. –

相关问题