Razgriz
Razgriz

Reputation: 7343

iOS 7 - Local Declaration hides instance variable when calling results from a database query

I have this function that takes results from a database query. I am using the FMDB Library to query the database. However, I have a feeling that I am not properly transferring the query results to my mutable array. Here is my fucntion

-(NSArray *)initializeGroupIDArray{
    // Getting the database path.
    NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsPath = [paths objectAtIndex:0];
    NSString *dbPath = [docsPath stringByAppendingPathComponent:@"itemList.db"];

    FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
    [database open];

    NSString *sqlSelectQuery = @"SELECT DISTINCT GROUPID FROM ItemList";

    // Query result
    FMResultSet *resultsWithNameLocation = [database executeQuery:sqlSelectQuery];

    while([resultsWithNameLocation next]) {
        NSString *itemName = [NSString stringWithFormat:@"%@",[resultsWithNameLocation stringForColumn:@"GROUPID"]];
    
        // loading your data into the array, dictionaries.
        NSLog(@"GroupID = %@", itemName);
        [groupArray addObject:itemName];
    }
    [database close];

    NSArray *groupID;
    [groupID = groupArray copy];

    return groupID;
}

A warning comes up at the lines NSLog(@"GroupID = %@", itemName); and [groupArray addObject:itemName]; at it says that the local declaration of itemName hides instance variable. I have a feeling that this is the culprit behind me not being able to properly add the results to my array. What am I doing wrong?

Upvotes: 4

Views: 694

Answers (1)

bneely
bneely

Reputation: 9093

The usage of itemName in the code you shared looks correct to me. The error message indicates that your class has an instance variable named itemName, which would make your usage of itemName in the NSLog and addObject: cases ambiguous (as two variables, one in your local scope, and one in your class scope, would be sharing the same variable name).

Fix this issue by choosing a new unique name for your NSString *itemName variable within this method.

Upvotes: 5

Related Questions