2012-06-14 2 views
1

나는 내 애플 리케이션의 다양한 장소에서 필요한 몇 가지 변수를 관리하는 싱글 톤을 가지고있다.EXC_BAD_ACCESS 싱글 톤을 사용할 때 코드 = 2

#import "General.h" 

static General *sharedMyManager = nil; 

@implementation General 

@synthesize user; 
@synthesize lon; 
@synthesize lat; 
@synthesize car; 
@synthesize firstmess; 
@synthesize firstfrom; 
@synthesize numcels; 

#pragma mark Singleton Methods 

+ (id)sharedManager { 
static dispatch_once_t onceToken; 
dispatch_once(&onceToken, ^{ 
    if (sharedMyManager == nil) { 
     sharedMyManager = [[self alloc] init]; 
    } 
}); 

return sharedMyManager; 
} 

- (id)init { 
if (self = [super init]) { 
    user = [[NSString alloc] initWithString:@"vacio"]; 
    numcels=0; 
} 
return self; 
} 

- (void)dealloc { 
// Should never be called, but just here for clarity really. 
} 

@end 

내가 채팅 내 응용 프로그램의 일부 화면 메시지에 존재하는있는 TableView에서 사용이 일반라는 싱글이다. 내 말은, 앱이 메시지를 받거나 보낼 때마다 var "numcels"에 1을 더하고, numberOfRowsInSection 메서드가 반환하는 값입니다.

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
General *general = [General sharedManager]; 
return *(general.numcels); //It freezes here 
} 

문제이며, 내가 프로그램을 실행할 때, 그것은 EXC_BAD_ACCESS 코드 = 2 말, 주석 라인에서 정지. 문제는 싱글 톤과 같을 수도 있지만 정확히 어디에 있는지 모른다.

어떤 도움이 필요합니까? 미리 감사드립니다.

------- 편집 --------

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
NSLog(@"Hemos entrado en cellForRowAtIndexPath"); 
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"]; 
if(!cell){ 
UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCell"]; 
} 
General *general = [General sharedManager]; 
NSString *text=general.firstmess;//it crashes now here 
NSString *remite=general.firstfrom; 
[[cell textLabel]setText:remite]; 
[[cell detailTextLabel] setText:text]; 

return cell; 
} 

그리고 요청에 의해 General.h :

#import <Foundation/Foundation.h> 

@interface General : NSObject { 
NSString *user; 
double lat; 
double lon; 
} 

@property (nonatomic, retain) NSString *user; 
@property (assign, nonatomic) double lat; 
@property (assign, nonatomic) double lon; 
@property (assign, nonatomic) Boolean car; 
@property (assign, nonatomic) NSString *firstmess; 
@property (assign, nonatomic) NSString *firstfrom; 
@property (assign, nonatomic) int numcels; 

+ (id)sharedManager; 

@end 
+0

왜 하시겠습니까 * (general.numcels); ..? 나는 별 뒤에있는 아이디어가 무엇을 의미합니까? –

+0

내가 그 일을하지 않으면 "정수 변환에 대한 호환되지 않는 포인터 ..."라는 경고가 표시됩니다. – Fustigador

+0

.h를 표시 할 수 있습니까? –

답변

0

첫 번째 문제 (Ankit의 도움 덕분에)를 해결 한 후 EDIT 아래에 주석으로 표시된 행에 오류가 발생했습니다. 나는 단순히

@property (retain, nonatomic) NSString *firstmess; 

@property (nonatomc, assign) NSString *firstmess; 

을 변경 그리고 그것은 더 이상 충돌하지 않습니다.

감사합니다.

2

그것은 다음과 같이해야합니다 :

return general.numcels;

numcels

는 정수이고, 당신은,912을 적용 할 수 없습니다 그것에운영자.

관련 문제