我想創建一個應用程序,類似於Apple的BirdSighting示例(「您的第二個iOS應用程序」)。我使用的科目不是鳥類,每個科目都有一個標題(title
),一些核心主題(core
)和一些案例研究(datacase
)。當我嘗試在我的數據控制器(SubjectController.m
)中初始化某個主題時,從subject = [[Subject alloc]....
開始,我收到一條警告消息:「Expected」:'「。任何想法 - 與使用陣列有關嗎?NSArray和initWithTitle方法
Subject.h文件:
#import <Foundation/Foundation.h>
@interface Subject : NSObject
@property (nonatomic, copy) NSString *title;
@property (nonatomic, strong) NSArray *core;
@property (nonatomic, strong) NSArray *datacase;
-(id)initWithTitle:(NSString *)title core:(NSArray *)core datacase:(NSArray *)datacase;
@end
Subject.m文件:
#import "Subject.h"
@implementation Subject
-(id)initWithTitle:(NSString *)title core:(NSArray *)core datacase:(NSArray *)datacase
{
self = [super init];
if (self) {
_title = title;
_core = core;
_datacase = datacase;
return self;
}
return nil;
}
@end
SubjectController.h:
#import <Foundation/Foundation.h>
@class Subject;
@interface SubjectController : NSObject
@property (nonatomic, copy) NSMutableArray *masterSubjectList;
-(NSUInteger)countOfList;
-(Subject *)objectInListAtIndex:(NSInteger)theIndex;
-(void)addSubjectWithSubject:(Subject *)subject;
@end
SubjectController.m:
#import "SubjectController.h"
#import "Subject.h"
@interface SubjectController()
-(void)createSubjectList;
@end
@implementation SubjectController
-(id) init {
if (self = [super init]) {
[self createSubjectList];
return self;
}
return nil;
}
-(void)createSubjectList {
NSMutableArray *subjectList = [[NSMutableArray alloc] init];
self.masterSubjectList = subjectList;
Subject *subject;
subject = [[Subject alloc] initWithTitle:@"Maths" core:@"Introduction", @"Adding", @"Subtracting" datacase:@"Case 1", @"Case 2", @"Case 3", nil];
[self addSubjectWithSubject:subject];
}
-(void)setMasterSubjectList:(NSMutableArray *)newList {
if (_masterSubjectList != newList) {
_masterSubjectList = [newList mutableCopy];
}
}
-(NSUInteger)countOfList {
return [self.masterSubjectList count];
}
-(Subject *)objectInListAtIndex:(NSInteger)theIndex {
return [self.masterSubjectList objectAtIndex:theIndex];
}
-(void)addSubjectWithSubject:(Subject *)subject {
[self.masterSubjectList addObject:subject];
}
@end