ios - NSArray 和 initWithTitle 方法

标签 ios objective-c xcode uistoryboard

我正在尝试创建一个应用程序,类似于 Apple 的 BirdSighting 示例(“您的第二个 iOS 应用程序”)。我使用主题代替鸟类,每个主题都有一个标题 (title)、一些核心主题 (core) 和一些案例研究 (datacase).当我尝试在我的数据 Controller (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

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

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

最佳答案

您正在将以逗号分隔的 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 中找到。

关于ios - NSArray 和 initWithTitle 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18932834/

相关文章:

ios - 在单元格的 imageView 中为图像添加边框

ios - 使用 UIImageView+AFNetworking Xcode 7 加载图像

xcode - iOS LoadPresetDemo 示例代码中的 AU Preset (.aupreset) 资源路径问题

ios - MagicalRecord saveWithBlock 用法现在无法在 XCode 7 beta 5 下编译

ios - 核心数据不覆盖对象

ios - 禁用将其移动到上一个屏幕的 ios 应用程序的滑动功能

ios - 在 Xcode 9 中实现预处理器?

objective-c - TableView 无/不更新

ios - UIbarbutton蓝色而不是背景图片

objective-c - 显示占位符标记时如何控制 NSTextField 的文本颜色?