2012-05-12 32 views
0

所以我的代碼編譯,當我在xcode中運行我的iPhone應用程序時,它崩潰了吐出一大堆內存錯誤,抱怨壞訪問,我不知道這意味着什麼。所有IAM試圖做的是使用數組中的計數器移除我從超級視圖中創建的實驗室,結果是比我想象的要難得多。我很新的對象C和iPhone開發的任何幫助將不勝感激。從陣列中刪除不良訪問

#import "FlipsideViewController.h" 

@interface MainViewController : UIViewController <FlipsideViewControllerDelegate> 
{ 
    /*This stuff creates a timer */ 
    IBOutlet UILabel *opponentsBlue; 
    NSTimer *timer; 
    int redBlue; 
    int count; 
    /*Stuff for making a label creator */ 
    CGPoint startPoint; 
    int xStuff, yStuff; 

    /*array for storing wards*/ 
    NSMutableArray *wardArray; 

} 

@property CGPoint startPoint; 

- (IBAction)startRedBlue:(id)sender; 

- (IBAction)removeWard:(id) 
sender; 
- (void)countdown; 
-(id)init; 

@end 

#import "MainViewController.h" 

@interface MainViewController() 

@end 

@implementation MainViewController 

@synthesize startPoint; 

- (void)countdown 
{ 
    if (redBlue < 2) { 

     [timer invalidate]; 
     timer = nil; 
    } 
    redBlue -= 1; 
    opponentsBlue.text = [NSString stringWithFormat:@"%i", redBlue]; 
} 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *theTouch = [touches anyObject]; 
    startPoint = [theTouch locationInView:self.view]; 

} 

-(id)init{ 
    int count = 0; 
    return self; 
} 

- (IBAction)startRedBlue:(id)sender 
{ 
    UIButton *wardButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    wardButton.frame = CGRectMake((startPoint.x - 5), (startPoint.y - 5), 10, 10); 
    [wardButton setTitle:@"180" forState:UIControlStateNormal]; 
    //add targets and actions 
    /*[wardButton addTarget:self action:@selector() forControlEvents:<#(UIControlEvents)#>*/ 
    //add to a view 
    [self.view addSubview:wardButton]; 

    if (!wardArray) { 
     wardArray = [NSMutableArray array]; 
    } 
    if (wardArray){ 
     [self->wardArray addObject: wardButton]; 
     count++; 
    } 

    NSLog(@"This elemnt = %@", wardArray); 


} 
- (IBAction)removeWard:(id)sender 
{ 

    NSLog(@"The count is %@", count); 
    [[wardArray objectAtIndex:count] removeFromSuperview]; 

    count--; 

    NSLog(@"This elemnt = %@", wardArray); 


} 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

#pragma mark - Flipside View 

- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller 
{ 
    [self dismissModalViewControllerAnimated:YES]; 
} 

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([[segue identifier] isEqualToString:@"showAlternate"]) { 
     [[segue destinationViewController] setDelegate:self]; 
    } 
} 

@end 
+0

顯示您看到的確切錯誤消息和/或崩潰報告,並假設調試器停止您的程序,顯示它顯示的代碼中的哪一行。 –

回答

0

我在猜測這不是ARC(自動引用計數)。

wardArray不被保留,因此它在創建後很快就會被釋放。

您可以嘗試用[[NSMutableArray alloc] init]代替[NSMutableArray array]。這樣它只會泄漏內存而不是留下殭屍。

此外,您可能想要了解Objective-C內存管理。

+0

Nop那沒有工作的人我討厭內存錯誤問題 –

+0

事實證明問題是我試圖用NSLOG打印計數器感謝您的幫助,雖然 –