ios - 在 objective-c 中访问方法之外的变量

标签 ios nsarray global-variables

我有这个函数,它将数组的内容存储在三个不同的变量中。我的问题是如何从同一类中的另一个函数访问存储在 _company.address 中的内容。

-(NSArray *) csvArray2CompaniesArray:(NSArray *) csvArray
{
    int i=0;    

    NSMutableArray *ma = [[NSMutableArray alloc] init];

    for (NSArray * row in csvArray)
    {
        if (i>0)
        {
            Company *_company = [[Company alloc] init];

            _company.name = [row objectAtIndex:0];
            _company.address = [row objectAtIndex:1];
            _company.telephone = [row objectAtIndex:2];

            [ma addObject:_company];
        }
        i++;
    }
    return (NSArray *) ma;
}

提前谢谢你。

最佳答案

一个对象的可访问性不能在函数中是私有(private)的。要么将其声明为全局对象,要么在类的范围内声明它。在 .h 文件中更好

您可以将 functios 值用作:-

YourViewControllerWithFunction *accessFunc=[[YourViewControllerWithFunction  alloc]]init];

Company *_company=[accessFunc csvArray2CompaniesArray:youInputArray];

[_company objectAtIndex:intVallue];//Use in loop

关于ios - 在 objective-c 中访问方法之外的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10647453/

相关文章:

global-variables - 在java中使用应用范围变量

ios - 如何在 Spritekit 中创建一个计时器?

ios - UITableViewCell 中的 UIButton(在 UIView 内)单击事件不起作用

ios - 如何在以下 NSDictionary 中下钻?

ios - 使用对象属性从 NSArray 中过滤重复项

c - 全局数组问题不更新,C 编程

python - 当我将其设置为全局变量时在 python 中赋值之前引用的局部变量

ios - 在自定义类中初始化日期会导致错误 : "instance member cannot be used on type"

ios - 为什么我在使用 XCode 时不能拥有名为 'retain' 的属性?

objective-c - 有没有一种简单的方法可以将 NSString 拆分为字符数组?