开发者

Cocoa - Outputting Array Text

#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: (NSMutable开发者_StackOverflow中文版Array *) 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


Override NSObject's description method. Add something like this to QuestionItem.m

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", question];
}

You can build whatever string you want to return in the description method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜