1

我正在尝试创建一个应用程序,类似于 Apple 的 BirdSighting 示例(“您的第二个 iOS 应用程序”)。我使用的是主题而不是鸟类,每个主题都有一个标题 ( title)、一些核心主题 ( core) 和一些案例研究 ( datacase)。当我尝试在我的数据控制器 ( SubjectController.m) 中初始化一个主题时,我在以subject = [[Subject alloc]..... 任何想法 - 也许与使用数组有关?

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

主题.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

主题控制器.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

主题控制器.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
4

2 回答 2

1

您正在将逗号分隔的NSString文字列表作为参数传递给预期类型为NSArray实例的方法。与您想要的最接近的有效语法可能是:

subject = [[Subject alloc] initWithTitle:@"Maths" core:@[@"Introduction", @"Adding", @"Subtracting"] datacase:@[@"Case 1", @"Case 2", @"Case 3"]];

请注意,每个以逗号分隔的字符串列表现在都用方括号括起来,第一个括号前面有一个@,并且终止符nil从第二个列表中删除。这是 Objective-C 文字的语法,更多内容可以在Clang 文档中找到关于 Objective-C 文字的内容

于 2013-09-21T13:07:32.863 回答
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];
于 2013-09-21T12:58:36.787 回答