简体   繁体   English

NSMutableArray中的对象无法访问Objective-C

[英]Objects in NSMutableArray inaccesible Objective-C

//ARC is turned on in Xcode 4.2 //在Xcode 4.2中打开了ARC

A static function is created which executes a query and gets the values from the database SQLite. 创建了一个静态函数,该函数执行查询并从数据库SQLite获取值。 The array values returned by the function displayquery is an array of a no of mutable arrays which contain the records. 函数displayquery返回的数组值是一个包含记录的可变数组的数组。 I want to convert it into client objects and store the list list in an NSMutable object before returning it. 我想将其转换为客户端对象,并将列表列表存储在NSMutable对象中,然后再返回它。

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. 问题在于, lst数组中的第一个元素是NSString ,它不具有Client类所具有的任何属性,并且当您尝试分配给它时,会出现错误。 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. 不要在数组中添加@"Client"作为元素,因为它似乎不属于该元素。
  2. You don't need to “pre-add” the Client objects to the array. 您无需将“ Client对象“预添加”到阵列。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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