NSArray 和 initWithTitle 方法

Posted

技术标签:

【中文标题】NSArray 和 initWithTitle 方法【英文标题】:NSArray and initWithTitle method 【发布时间】:2013-09-21 12:51:44 【问题描述】:

我正在尝试创建一个应用程序,类似于 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

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

【问题讨论】:

【参考方案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

中找到

【讨论】:

【参考方案2】:

试试:

 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];

【讨论】:

以上是关于NSArray 和 initWithTitle 方法的主要内容,如果未能解决你的问题,请参考以下文章

AlertView 的 init 方法中的参数

NSArray 中的 iOS sdk NSArray 查找和合并

NSMutableArray和NSArray的相互转换

从 NSArray 获取单个属性的 NSArray

NSArray 和 NSDictionary

多节 UITableView 和 NSArray