NSMutableArray中的对象不可用Objective-C

时间:2011-12-13 15:55:37

标签: objective-c ipad

//在Xcode 4.2中打开了ARC

创建一个静态函数,它执行查询并从数据库SQLite中获取值。 函数displayquery返回的数组值是包含记录的no可变数组的数组。 我想将它转换为客户端对象,并在返回之前将列表列表存储在NSMutable对象中。

静态功能

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

1 个答案:

答案 0 :(得分:0)

问题是,lst数组中的第一个元素是NSString,它没有Client类所具有的任何属性,并且当您尝试分配时对它来说,你得到一个错误。您的代码中存在一些问题:

  1. 不要将@"Client"添加为数组中的元素,因为它似乎不属于那里。
  2. 您无需将Client个对象“预先添加”到数组中。只需创建它们并随时将它们添加到阵列中。
  3. 当您拥有数组数组时,查找项目时可能会更难理解每个索引的用途。我在代码中重命名了几个变量,因为它让我更容易理解。
  4. 我认为您的代码应该更像这样:

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