项目构建正常,但是一旦应用程序尝试运行它就会崩溃:
它(错误)已经显示如下。
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
// here i got Thread1: Program received signal: "SIGBRT"
}
}
我的代码是:
(我已经声明了collecter实例并导入了collector.h)
#import <UIKit/UIKit.h>
#import "Collector.h"
@interface ViewController : UIViewController{
Collector *model;
IBOutlet UILabel *totalStrings;
IBOutlet UILabel *totalNumbers;
}
-(void)collect:(UIButton *)sender;
@end
#import "ViewController.h"
#import "Collector.h"
@implementation ViewController
-(void)updateUI
{
totalNumbers.text = [NSString stringWithFormat:@"%d",model.totalNumberCount];
totalStrings.text = [NSString stringWithFormat:@"%d",model.totalStringCount];
}
-(IBAction)collect:(UIButton *)sender
{
if(!model) model = [[Collector alloc] init];
double doubleValue = [sender.titleLabel.text doubleValue];
if (doubleValue) {
[model collect:[NSNumber numberWithDouble:doubleValue]];
} else{
[model collect:sender.titleLabel.text];
}
[self updateUI];
}
@end
#import <Foundation/Foundation.h>
@interface Collector : NSObject{
NSMutableDictionary *counts;
}
-(void)collect:(id)anObject;
@property (readonly) int totalStringCount;
@property (readonly) int totalNumberCount;
@end
#import "Collector.h"
@interface Collector()
@property (readonly) NSMutableDictionary *counts;
@end
@implementation Collector
-(NSMutableDictionary *)counts
{
if (!counts) {
counts = [[NSMutableDictionary alloc] init];
}
return counts;
}
-(void)collect:(id)anObject
{
if ([anObject isKindOfClass:[NSString class]] || [anObject isKindOfClass:[NSNumber class]] ) {
NSNumber *existingCount = [self.counts objectForKey:anObject];
[self.counts setObject:[NSNumber numberWithInt: [existingCount intValue] + 1] forKey:anObject];
}
}
- (int) totalStringCount
{
int total = 0;
for (id key in self.counts) {
if ([key isKindOfClass:[NSString class]]) {
total +=[[self.counts objectForKey:key] intValue];
}
}
return total;
}
-(int)totalNumberCount
{
int total = 0;
for (id key in self.counts) {
if ([key isKindOfClass:[NSNumber class]]) {
total +=[[self.counts objectForKey:key] intValue];
}
}
return total;
}
@end
您认为导致此问题的原因是什么?
答案 0 :(得分:1)
查看此帖子如何设置NSZombieEnabled
。这将显示有关崩溃的更多详细信息,可帮助您调试问题: