我正在尝试创建一个应用程序,类似于Apple的BirdSighting示例(“您的第二个iOS应用程序”)。我使用的是主题而不是鸟类,每个主题都有一个标题(title
),一些核心主题(core
)和一些案例研究(datacase
)。当我尝试在我的数据控制器(SubjectController.m
)中初始化主题时,我会在以subject = [[Subject alloc]....
开头的行上收到警告“预期':'”。任何想法 - 可能与使用Arrays有关吗?
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
答案 0 :(得分:1)
您将以逗号分隔的NSString
文字列表作为参数传递给预期类型为NSArray
实例的方法。您想要的最接近的有效语法可能是:
subject = [[Subject alloc] initWithTitle:@"Maths" core:@[@"Introduction", @"Adding", @"Subtracting"] datacase:@[@"Case 1", @"Case 2", @"Case 3"]];
请注意,每个以逗号分隔的字符串列表现在都用方括号括起来,第一个括号前面有@
,终止nil
将从第二个列表中删除。这是Objective-C文字的语法,更多内容可以在Clang documentation on Objective-C literals
答案 1 :(得分:0)
尝试:
NSArray *core = [NSArray arrayWithObjects::@"Introduction", @"Adding", @"Subtracting", nil];
NSArray *datasource = [NSArray arrayWithObjects:@"Case 1", @"Case 2", @"Case 3", nil];
subject = [[Subject alloc] initWithTitle:@"Maths" core:core datacase:datasource];
而不是:
subject = [[Subject alloc] initWithTitle:@"Maths" core:@"Introduction", @"Adding", @"Subtracting" datacase:@"Case 1", @"Case 2", @"Case 3", nil];