2013-09-21 67 views
1

我想創建一個應用程序,類似於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 

回答

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

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