1

I have a Singleton to manage some variables I need in various places in my app. This is the singleton, called General:

#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

I use it in a TableView that present in screen messages of a part of my app that is a chat. I mean, everytime the app receives or send a message, i add 1 to the var "numcels", and that is the value that numberOfRowsInSection method returns.

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

The problem is, when i run the program, it freezes at the commented line, saying EXC_BAD_ACCESS code=2. I guess that the problema may be with the singleton, but don't know where it is exactly.

Any help? Thank you in advance.

-------EDIT--------

-(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;
}

And the General.h, by request:

#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
Fustigador
  • 6,339
  • 12
  • 59
  • 115

2 Answers2

2

It should be like the following:

return general.numcels;

numcels is an integer and you cannot apply the * operator to it.

Fran Sevillano
  • 8,103
  • 4
  • 31
  • 45
0

After solving the first issue (thanks to the kind help of Ankit), it crashed in the line I have commented below the EDIT. I simply changed

@property (nonatomc, assign) NSString *firstmess;

to

@property (retain, nonatomic) NSString *firstmess;

And it doesn't crash anymore.

Thank you!

Fustigador
  • 6,339
  • 12
  • 59
  • 115