我有两个类,比如说A和B.我在A类中有一个可变数组_arrayOfPaths现在我试图在类b中访问该数组.如下所示:
A *testModel = [[A alloc]init];
NSMutableArray *array = [testModel.arrayOfPaths mutableCopy];
Run Code Online (Sandbox Code Playgroud)
现在,当我NSLog这个新制作的数组时,它显示为null.如果我打印testModel.arrayOfPaths然后它也显示为null.
我的问题是:
_arrayOfPathsB班?UITableViewController.1.在这里,您只是创建一个新的A实例,而不是访问现有的A实例来获取B实例中的数组.所以只有它显示空值.
2.查看第二个问题的代码....
ClassA.h
@interface ClassA:UIViewController
@property (nonatomic, retain) NSMutableArray *arrayOfPaths;
@end
Run Code Online (Sandbox Code Playgroud)
ClassA.m
@implementation ClassA
@synthesize arrayOfPaths = _arrayOfPaths;
-(id)init
{
self = [super init];
if(self)
{
self.arrayOfPaths = [[NSMutableArray alloc] initWithObjects:@"1",@"2", nil];
//Delegation process
ClassB *bInstance = [[ClassB alloc] init];
[bInstance setAReference:self];
//Do your stuff of presenting ClassB instance here.....
}
}
@end
Run Code Online (Sandbox Code Playgroud)
ClassB.h
#import "ClassA.h"
@interface ClassB:UITableViewController
@property (nonatomic, copy) ClassA *aReference;
@end
Run Code Online (Sandbox Code Playgroud)
ClassB.m
@implementation ClassB
@synthesize aReference = _aReference;
-(void)viewDidLoad
{
NSLog(@"Array from class ClassA %@", self.aReference.arrayOfPaths);
}
@end
Run Code Online (Sandbox Code Playgroud)
ClassB ViewController只能在这里由ClassA实例化.
3.您需要使用委托概念来实现您的要求.请参阅上面的示例代码
4. objective -c不支持多重继承.
| 归档时间: |
|
| 查看次数: |
6470 次 |
| 最近记录: |