2011-08-31 62 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 .. –

相關問題