daunmi
daunmi

Reputation: 17

Access a variable outside the method in objective-c

I have this function that is storing the contents of an array in three different variables. My question is how can I access to the contents stored in _company.address from another function in the same class.

-(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;
}

Thank you in advance.

Upvotes: 1

Views: 848

Answers (2)

Abhishek Singh
Abhishek Singh

Reputation: 6166

You cannot the accessibility of an object is private in a function.Either you declare it as global or declare it in class's scope.Better in .h file

You can use the functios value as :-

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

Company *_company=[accessFunc csvArray2CompaniesArray:youInputArray];

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

Upvotes: 1

jimpic
jimpic

Reputation: 5520

You should correct the return value of your function to NSMutableArray or make a copy to get the NSArray of the NSMutableArray you created.

After that, you can access the contents of your array from anywhere inside your class like:

for (Company *c in [self csvArray2CompaniesArray:csvarray])
{
    NSLog(@"%@", c.address);
}

Upvotes: 0

Related Questions