简体   繁体   中英

Objects in NSMutableArray inaccesible Objective-C

//ARC is turned on in Xcode 4.2

A static function is created which executes a query and gets the values from the database SQLite. The array values returned by the function displayquery is an array of a no of mutable arrays which contain the records. I want to convert it into client objects and store the list list in an NSMutable object before returning it.

static function

+ (NSMutableArray*) list
{

    NSString *querySQL = //some query;

    NSMutableArray *values = [Client displayQuery:querySQL numberOfColumns:7];

    NSMutableArray *lst = nil;

    if (values != nil)
    {
        lst = [NSMutableArray arrayWithObject:@"Client"];

        for (int i = 0; i<[[values objectAtIndex:0] count] ; i++) 
        {    
            [lst addObject:[Client new ]];
        }
        for (int i = 0; i<[[values objectAtIndex:0] count] ; i++) 
        {    

            Client *aClient = [lst objectAtIndex:i];
            //error occurs during the execution of this line.
            //all properties of Class client are (retain,nonatomic)
            aClient.idClient = [[values objectAtIndex:0]objectAtIndex:i];
            aClient.prenom = [[values objectAtIndex:1]objectAtIndex:i];
            aClient.name = [[values objectAtIndex:2]objectAtIndex:i];
            aClient.address = [[values objectAtIndex:3]objectAtIndex:i];
            aClient.telephone = [[values objectAtIndex:4]objectAtIndex:i];
            aClient.email = [[values objectAtIndex:5]objectAtIndex:i];
            aClient.weight = [[values objectAtIndex:6]objectAtIndex:i];
            [lst addObject: aClient];

        }
    }
    return lst;
}

The problem is that the first element in your lst array is an NSString , which does not have any of the properties that your Client class has, and when you try to assign to it, you get an error. There are a few issues in your code:

  1. Don't add @"Client" as an element in your array, since it doesn't seem like it belongs there.
  2. You don't need to “pre-add” the Client objects to the array. Just create them and add them to the array as you go.
  3. When you have arrays of arrays, it can become harder to understand what each index is for when you look up items. I've renamed a couple of variables in your code because it made it easier for me to understand.

I think your code should look more like this:

+ (NSMutableArray*) list
{
    NSString *querySQL = //some query;
    NSMutableArray *columns = [Client displayQuery:querySQL numberOfColumns:7];
    NSMutableArray *lst = nil;

    if (columns == nil)
        return nil;

    NSUInteger count = [[columns objectAtIndex:0] count];
    lst = [NSMutableArray arrayWithCapacity:count];

    for (NSUInteger row = 0; row < count; row++) 
    {    
        Client *aClient = [Client new];

        aClient.idClient = [[columns objectAtIndex:0] objectAtIndex:row];
        aClient.prenom = [[columns objectAtIndex:1] objectAtIndex:row];
        aClient.name = [[columns objectAtIndex:2] objectAtIndex:row];
        aClient.address = [[columns objectAtIndex:3] objectAtIndex:row];
        aClient.telephone = [[columns objectAtIndex:4] objectAtIndex:row];
        aClient.email = [[columns objectAtIndex:5] objectAtIndex:row];
        aClient.weight = [[columns objectAtIndex:6] objectAtIndex:row];

        [lst addObject: aClient];
    }

    return lst;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM