Florian_accessdev
Florian_accessdev

Reputation: 89

Why the variables are NULL?

I got a problem to develop an app for iPhone. I have to do a quiz. The quiz is in a NSArray and each question has a title, some answers and a score by answers. All these informations are in a NSMutableArray.

When I try to print the value "score" for exemple, I obtain a NULL and not the number.

Example: MYCOMMON.H

#import <Foundation/Foundation.h>
#import "FBConnect/FBConnect.h"


@interface MyCommon : NSObject <FBRequestDelegate,FBDialogDelegate,FBSessionDelegate>
 {

    NSArray *quizz;
    int iCurrentQuestion;
    int iScore;
    int iRep;

    NSArray *permissions;

};

+ (MyCommon *)Singleton;


@property (nonatomic, retain) NSArray *quizz;
@property (assign) int iCurrentQuestion;
@property (retain,nonatomic) NSArray *permissions;
@property (assign) int iScore;
@property (assign) int iRep;
@end


   MYCOMMON.M
@implementation MyCommon

static MyCommon * SingletonManager = nil;

@synthesize iCurrentQuestion, iScore, iRep;
@synthesize quizz;


-(id)init {

    if (self = [super init]) {

    iCurrentQuestion = 0;
        iRep = 0;
        iScore = 0;

        permissions =  [[NSArray arrayWithObjects:@"publish_stream",nil] retain];

    }
    return self;

}

-(void)dealloc {

    // Do things
    [ super dealloc ];

}

- (int)GetScoreForReponse 
{    


    MyCommon *pCommon = [MyCommon Singleton];

    NSLog(@"GetScoreForReponse iScore : %@, pCommon.iScore);

    NSDictionary *dataItem = [quizz objectAtIndex:iCurrentQuestion];


    int add_score = 0;

    if (iRep == 1)
    {
            add_score = [[dataItem objectForKey:@"score1"]intValue];
    }
    else if (iRep == 2)
    {

         add_score =  [[dataItem objectForKey:@"score2"]intValue];
    }
    else if (iRep == 3)
    {
         add_score =  [[dataItem objectForKey:@"score3"]intValue];
    }
    else if (iRep == 4)
    {
         add_score =  [[dataItem objectForKey:@"score4"]intValue];
    }
    else if (iRep == 5)
    {
         add_score =  [[dataItem objectForKey:@"score5"]intValue];
    }
    else
        NSLog(@"error !");

    return add_score;
}

Thank for help !

Upvotes: 0

Views: 96

Answers (1)

Joe
Joe

Reputation: 57179

You are attempting to print the description of an Objective-C object referenced by the address stored in iScore by using %@, use %d instead. If iScore was non zero that would more than likely result in a crash rather than NULL.

NSLog(@"GetScoreForReponse iScore : %d", pCommon.iScore);

Here is a list of String Format Specifiers.

Upvotes: 2

Related Questions