Ios 目标C访问其他类变量值
我有两门课,科目课和章节课 我在Subject类中分配了一个Ios 目标C访问其他类变量值,ios,objective-c,Ios,Objective C,我有两门课,科目课和章节课 我在Subject类中分配了一个NSMutable数组,如 @property(nonatomic,retain)NSMutableArray *htmlURL; 在.m文件中: htmlURL = [[NSMutableArray alloc]init]; [htmlURL addObject:@"1", @"2"]; 在章节课上我是这样做的 Subject *sub = [[Subject alloc] init]; NSLog(@"%@"sub.htmlUR
NSMutable
数组,如
@property(nonatomic,retain)NSMutableArray *htmlURL;
在.m文件中:
htmlURL = [[NSMutableArray alloc]init];
[htmlURL addObject:@"1", @"2"];
在章节课上我是这样做的
Subject *sub = [[Subject alloc] init];
NSLog(@"%@"sub.htmlURL);
+ (Subject *) sharedInstance
{
@synchronized([Subject class]) {
if (!instance) {
instance = [[Subject alloc] init];
}
}
return instance;
}
输出为空。。。我想要Chapter类中的htmlurl数组值。。我这里缺少的东西。您必须连接这两个类 您正在创建两个不同的对象,然后尝试访问另一个对象中一个对象的成员的值。这样做 当您从主题课来到章节课时,您可能正在推送或演示章节课
//in Subject Class, while creating Chapter class
Chapter* newClass = [Chapter alloc] init];
newClass.parentObject = self;
//code for transitioning to chapter controller
// in Chapter Class, create a parent object
@property (nonatomic, assign) Subject* parentObject;
或者如果主题是一个广义的公共类
然后您可以创建一个方法来返回它的公共实例。像这样的,
Subject *sub = [[Subject alloc] init];
NSLog(@"%@"sub.htmlURL);
+ (Subject *) sharedInstance
{
@synchronized([Subject class]) {
if (!instance) {
instance = [[Subject alloc] init];
}
}
return instance;
}
然后访问主题类成员,如下所示:
Subject *sub = [Subject sharedInstance];
NSLog(@"%@"sub.htmlURL);
您必须显示更多代码,在.m文件中,您在哪里alloc/init数组?在init方法中?你的目标是哪个操作系统?我正在解析XML,所以在parserDidStartDocument方法中,我是alloc/init数组..主题是第章的父对象吗?