我遇到了为iPhone开发应用程序的问题。 我必须做一个测验。 测验是在NSArray中,每个问题都有一个标题,一些答案和一个答案得分。 所有这些信息都在NSMutableArray中。
当我尝试打印值“得分”为例时,我得到一个NULL而不是数字。
实施例: 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;
}
感谢您的帮助!
答案 0 :(得分:2)
您正在尝试使用%@打印存储在iScore中的地址引用的Objective-C对象的描述,而是使用%d。如果iScore非零,则很可能导致崩溃而不是NULL。
NSLog(@"GetScoreForReponse iScore : %d", pCommon.iScore);
以下是String Format Specifiers的列表。