我不知道为什么会出现此错误。我有以下课程:
#import <Foundation/Foundation.h>
#import "Answer.h"
@interface Question : NSObject {
NSString* qId;
NSString* qTitle;
NSString* qNumber;
NSString* sectionId;
NSString* qType;
Answer* answer;
}
@property (nonatomic, retain) NSString* qId;
@property (nonatomic, retain) NSString* qTitle;
@property (nonatomic, retain) NSString* qNumber;
@property (nonatomic, retain) NSString* sectionId;
@property (nonatomic, retain) NSString* qType;
@property (nonatomic, retain) Answer* answer;
#import "Question.h"
@implementation Question
@synthesize qId, qTitle, qNumber, sectionId, qType, answer;
-(id)init
{
if (self = [super init])
{
// Initialization code here
answer = [[Answer alloc]init];
}
return self;
}
-(void) dealloc{
[answer release];
[super dealloc];
}
@end
我在以下类中有一个类型为Question的属性:
#import <Foundation/Foundation.h>
#import "TextInputQuestion.h"
@interface TextInputQuestionViewController : UIViewController {
Question *question;
IBOutlet UILabel *questionTitle;
IBOutlet UILabel *inputLabel;
IBOutlet UITextField *inputAnswer;
}
@property (nonatomic, retain) UILabel *questionTitle;
@property (nonatomic, retain) UILabel *inputLabel;
@property (nonatomic, retain) Question *question;
@property (nonatomic, retain) UITextField *inputAnswer;
- (void) addButton:(id)sender isLast:(BOOL)last;
- (void) setQuestionId:(NSString*)quId withTitle:(NSString*)quTitle number:(NSString*)quNum section:(NSString*)sId questionType:(NSString*)qt;
@end
我在.m文件中合成Question* question
。
在我的QuestionnaireVieController类中:
#import <UIKit/UIKit.h>
#import "JSONKit.h";
#import "dbConnector.h"
#import "SliderQuestionViewController.h";
#import "TextInputQuestionViewController.h";
#import "MainMenuProtocol.h";
@interface QuestionnaireViewController : UIViewController {
NSDictionary* questions;
NSMutableArray* questionArray;
NSMutableArray* answerArray;
dbConnector* db;
NSInteger currQNum;
NSString* qaTitle;
NSString* secId;
id<MainMenuProtocol>delegate;
}
@property(nonatomic, retain) NSDictionary* questions;
@property(nonatomic, retain) NSMutableArray* questionArray;
@property(nonatomic, retain) NSMutableArray* answerArray;
@property(nonatomic, retain) dbConnector* db;
@property(nonatomic) NSInteger currQNum;
@property(nonatomic, retain) NSString* qaTitle;
@property(nonatomic, retain) NSString* secId;
@property(nonatomic, retain) id <MainMenuProtocol> delegate;
-(void) setQuestions;
-(void) startQuestion:(NSInteger)index isLast:(BOOL)last;
-(void) loadQuestions;
-(void) initialise;
-(void) finishQuestionnaire:(id)sender;
-(void) switchViews:(id)sender;
@end
我在.m文件中使用以下方法用不同类型的viewcontroller填充questionArray:
//assigns JSON to question objects
-(void) setQuestions {
questionArray = [[NSMutableArray alloc] init];
for (NSDictionary *q in self.questions) {
/* Create Question object and populate it */
id question;
if([[q objectForKey:@"type"] isEqualToString:@"Slider"]){
question = [[SliderQuestionViewController alloc]init];
//set min max values
}else if([[q objectForKey:@"type"] isEqualToString:@"Option"]){
}else if([[q objectForKey:@"type"] isEqualToString:@"TextInput"]){
question = [[TextInputQuestionViewController alloc]init];
}else if([[q objectForKey:@"type"] isEqualToString:@"ImagePicker"]){
}else{
//comments
}
[questionArray addObject:question];
[question setQuestionId:[q objectForKey:@"questionId"] withTitle:[q objectForKey:@"question"] number:[q objectForKey:@"questionNumber"] section:[q objectForKey:@"sectionId"] questionType: [q objectForKey:@"type"]];
[question release];
}
}
当我在上面的.m文件中执行以下操作时:
NSLog(@"%@", [questionArray objectAtIndex:0]);
我明白了:
<TextInputQuestionViewController: 0x6062e90>
当我尝试做的时候:
NSLog(@"%@", [questionArray objectAtIndex:0].question);
我收到错误:
request for member 'question' in something not a structure or union
为什么它不能访问问题变量?特别是当它实际记录我的控制器对象时。我在这里错过了一些东西......
答案 0 :(得分:0)
你正陷入疯狂的点符号。切换到这个,我打赌它有效(或者至少你得到一个有用的错误):
NSLog(@"%@", [[questionArray objectAtIndex:0] question]);
Dot-notation是ObjC2中的一个鼓舞人心的补充,编译器仍然不能总是想出来,特别是当你试图将消息传递给id
时(如objectAtIndex:
}返回)。编译器感到困惑,认为这是你用于struct
的点而不是伪装的消息,并且保释。
修改强>
foo.bar = baz;
=> [foo setBar:baz];
foo.bar.baz
=> [[foo bar] baz];
这,顺便说一句,是实际的Objective-C。点符号只是一种草率的写作方式。特别重要的是要了解foo.bar=baz
完全“将邮件setBar:
发送给foo
。”它的默认行为是使用声明的setter语义分配属性,但这只是默认值。您可以覆盖setBar:
来执行任何操作。您甚至可以将消息调度系统重新配置为不调用方法setBar:
(这不是一个模糊的功能,它由Core Data和Core Animation使用)。 Objective-C是一种动态语言,不幸的是,点符号通过使它看起来像Java而变得混乱。
答案 1 :(得分:0)
如果您只需要引用Question对象,那么您可以定义一个类QuestionViewController,例如包含Question对象,并在SliderQuestionViewController和TextInputQuestionViewController中继承此类,当您想要从数组中获取对象时,您可以将它向下转换为QuestionViewController类型的对象:
Question * q = ((QuestionViewController *)[questionArray objectAtIndex:0]).question;
q.id = qId;
NSLog(@"%@", q.qId);
或者您可以简单地写一下:
Question * q = (Question*)[[questionArray objectAtIndex:0] question];
q.id = qId;
NSLog(@"%@", q.qId);
答案 2 :(得分:0)
您是否在视图控制器中包含了Question.h?
即。
#include "Question.h"