#import<Foundation/Foundation.h><br/>
#import "QuestionItem.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// NSMutableArray *itemQuestion = [[NSMutableArray alloc] init];
// QuestionItem *newQuestion = [[QuestionItem alloc] init];
[QuestionItem aQuestion:@"Question to be answered" Ans: [NSMutableArray arrayWithObjects: @"Answer", @"Distractor1", @"Distractor2", nil]];
// [newQuestion initWithQuestion:@"Twas Brillig"];
// [newQuestion QuestionInstance:@"Test"];
// [newQuestion initWithAnswerMatrix: [NSMutableArray arrayWithObjects: @"First", @"Second", nil]];
// [newQuestion AnswerMatrixInstance: [NSMutableArray arrayWithObjects: @"First", @"Second", nil]];
// [newQuestion initWithQuestionAndAnswer:@"Jumping Jacks" Answer:[NSMutableArray arrayWithObjects: @"First", @"Second", nil]];
// NSLog(@"Hello, World!");
[pool drain];
return 0;
}
//
// QuestionItem.h
// NewQuestions
// I shouldn't be moving around mutables?
// Use NSCopy(ing) instead?
#import <Foundation/Foundation.h>
@interface QuestionItem : NSObject {
NSString *question;
NSMutableArray *answerMatrix;
NSDate *dateCreated;
}
@property (nonatomic, copy) NSString *question;
@property (nonatomic, copy) NSMutableArray *answerMatrix;
+ (id) aQuestion: (NSString *) qClassParam Ans: (NSMutableArray *) aClassParam;
- (id) init;
// - (id) initWithQuestion:(int)question;
- (id) initWithAnswerMatrix:(NSMutableArray *) answerMatrix;
- (id) initWithQuestionAndAnswer:(NSString *) question Answer:(NSMutableArray *)answerMatrix;
- (id) QuestionInstance: (NSString *) qInstanceParam;
- (id) AnswerMatrixInstance:(NSMutableArray *) amInstanceParam;
@end
//
// QuestionItem.m
// NewQuestions
//
#import "QuestionItem.h"
@implementation QuestionItem
@synthesize question, answerMatrix;
+ (id) aQuestion: (NSString *) qClassParam Ans: (NSMutableArray *) aClassParam {
QuestionItem *newQuestionItem = [[self alloc] initWithQuestionAndAnswer:qClassParam Answer: aClassParam];
return newQuestionItem;
}
- (id) init {
if((self = [super init])) {
/* initializations go here */
}
return self;
}
- (id) initWithQuestion: questionParam {
[self init];
[self QuestionInstance: questionParam];
return self;
}
- (id) initWithAnswerMatrix: (NSMutableArray *) answerMatrixParam {
[self init];
[self AnswerMatrixInstance:answerMatrixParam];
return self;
}
- (id) initWithQuestionAndAnswer: (NSString *) questionParam Answer: (NSMutableArray *) answerMatrixParam {
[self init];
[self QuestionInstance: questionParam];
[self AnswerMatrixInstance:answerMatrixParam];
return self;
}
- (id) QuestionInstance: (NSString *) qInstanceParam {
NSLog(@"QuestionInstance: %@", qInstanceParam);
return qInstanceParam;
}
- (id) AnswerMatrixInstance: (NSMutableArray *) amInstanceParam {
for (int i=0; i < [amInstanceParam count]; i++) {
NSLog(@"AnswerMatrixInstance: %@", [amInstanceParam objectAtIndex:i]);
}
return amInstanceParam;
}
@end
答案 0 :(得分:2)
覆盖NSObject的描述方法。将这样的内容添加到QuestionItem.m
- (NSString *)description
{
return [NSString stringWithFormat:@"%@", question];
}
您可以在描述方法中构建要返回的任何字符串。