我遇到了一个问题,我正在尝试从一个dataFile读取一个数组。
我有一个dataFile.h和dataFile.m
dataFile.h
#import <Foundation/Foundation.h>
@interface dataFile : NSObject
@enddataFile.m
#import "dataFile.h"
@implementation dataFile
-(void)Data
{
NSArray * myArray2 = [NSArray arrayWithObjects:@"f",@"b",@"z",nil];
}
@end我有另一个ViewController,它应该从dataFile.m中读取myArray2
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *Label;
@endViewController.m
#import "ViewController.h"
#import "dataFile.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// The code here doesn't seem to be working to read myArray2
_Label.text = [myArray2 indexOfObject:2];
}基本上,应该发生的事情是,ViewController将调用myArray2,然后将其设置为标签的文本。
然而,它似乎被困在阅读点。因为在没有Xcode抛出错误的情况下使用代码是不可能的。
发布于 2015-06-25 21:30:08
您需要将NSArray从接口中的dataFile中公开,并在videDidLoad中使用它。在您的示例中,在数据方法之外定义的本地NSArray不可见。以下是变化的样子。
dataFile.h
#import <Foundation/Foundation.h>
@interface dataFile : NSObject
+(NSArray *)Data;
@enddataFile.m
#import "dataFile.h"
@implementation dataFile
+(NSArray *)Data
{
return [NSArray arrayWithObjects:@"f",@"b",@"z",nil];
}
@endViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *Label;
@endViewController.m
#import "ViewController.h"
#import "dataFile.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_Label.text = [[dataFile Data] indexOfObject:2];
}
@endhttps://stackoverflow.com/questions/31060877
复制相似问题