Objective c 已定义属性,但无法访问它

Objective c 已定义属性,但无法访问它,objective-c,Objective C,我在一个类中声明了一个@property站点列表。我无法从另一个类访问站点列表,即使我已经导入了该类的.h文件。我所做的只是将listOfSites的一个实例更改为属性 这是另一个类slTableViewController的代码,我在其中发送消息以获取listOfSites: - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if([[segue identifier] isEqualToS

我在一个类中声明了一个@property站点列表。我无法从另一个类访问站点列表,即使我已经导入了该类的.h文件。我所做的只是将listOfSites的一个实例更改为属性

这是另一个类slTableViewController的代码,我在其中发送消息以获取listOfSites:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if([[segue identifier] isEqualToString:@"viewSitesSegue"])  {
        NSLog(@"viewSitesSegue");

        //  get list of sites
        slSQLite *dbCode = [[slSQLite alloc] init];
        [dbCode getListOfSites];

        //  put site data into array for display
        slSQLite *item = [[slSQLite alloc] init];
        NSLog(@"\n2-The array listOfSites contains %d items", item.listOfSites.count);
-  (void) getListOfSites {

    FMDatabase *fmdb = [FMDatabase databaseWithPath:databasePath];  // instantiate d/b
    if(![fmdb open])  {
        NSLog(@"\nFailed to open database");
        return;
    }

    NSMutableArray *listOfSites = [[NSMutableArray alloc] init];

    FMResultSet *rs = [fmdb executeQuery: @"SELECT SITE_ID, SITE_DESC, DATE FROM SiteData WHERE SITE_ID <> '0'"];
    while([rs next])  {
        sArray *sa = [[sArray alloc] init];
        sa.sSiteID = [rs stringForColumnIndex:0];
        sa.sJobDesc = [rs stringForColumnIndex:1];
        sa.sJobDate = [rs stringForColumnIndex:2];

        [listOfSites addObject:sa];  //  add class object to array
    }

    NSLog(@"\nThe array listOfSites contains %d items", listOfSites.count);

    [fmdb close];
}
以下是网站列表的代码:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if([[segue identifier] isEqualToString:@"viewSitesSegue"])  {
        NSLog(@"viewSitesSegue");

        //  get list of sites
        slSQLite *dbCode = [[slSQLite alloc] init];
        [dbCode getListOfSites];

        //  put site data into array for display
        slSQLite *item = [[slSQLite alloc] init];
        NSLog(@"\n2-The array listOfSites contains %d items", item.listOfSites.count);
-  (void) getListOfSites {

    FMDatabase *fmdb = [FMDatabase databaseWithPath:databasePath];  // instantiate d/b
    if(![fmdb open])  {
        NSLog(@"\nFailed to open database");
        return;
    }

    NSMutableArray *listOfSites = [[NSMutableArray alloc] init];

    FMResultSet *rs = [fmdb executeQuery: @"SELECT SITE_ID, SITE_DESC, DATE FROM SiteData WHERE SITE_ID <> '0'"];
    while([rs next])  {
        sArray *sa = [[sArray alloc] init];
        sa.sSiteID = [rs stringForColumnIndex:0];
        sa.sJobDesc = [rs stringForColumnIndex:1];
        sa.sJobDate = [rs stringForColumnIndex:2];

        [listOfSites addObject:sa];  //  add class object to array
    }

    NSLog(@"\nThe array listOfSites contains %d items", listOfSites.count);

    [fmdb close];
}
为了能够从其他类访问listOfSites,您需要将其设置为该类的属性:

在MyClass.h文件中

然后必须在MyClass.m文件中进行合成

现在,如果在另一个类中导入MyClass.h,则可以访问属性listOfSites。例如,在另一个文件OtherClass.m中:

您在发布的代码中犯了很多错误。即使假设您正确地将listOfSites声明为属性

首先,更改行:

NSMutableArray *listOfSites = [[NSMutableArray alloc] init];
进入:

第二,在以下方面:

slSQLite *dbCode = [[slSQLite alloc] init]; // Create an object
[dbCode getListOfSites]; // Generate the content of listOfSites

//  put site data into array for display
slSQLite *item = [[slSQLite alloc] init]; // Create another object
NSLog(@"\n2-The array listOfSites contains %d items", item.listOfSites.count); // Log the length of listOfSites in this new object
您正在为对象dbCode生成listOfSites的内容,并检查项目中listOfSites的长度,该项目是不同的对象,并且您没有为其生成listOfSites的内容

请尝试以下操作:

slSQLite *dbCode = [[slSQLite alloc] init]; // Create an object
[dbCode getListOfSites]; // Generate the content of listOfSites
NSLog(@"\n2-The array listOfSites contains %d items", dbCode.listOfSites.count); // Log the length of listOfSites in dbCode

不工作。。。当我做一个NSLIT项目。ListFosiSt.Calt,它是零。请考虑这是一个单独的问题,并在这里发布所有相关信息。我的回答很笼统,所以我的回答在这篇文章中很有意义。那么你能编辑你的问题来发布你的代码并解释你在哪里做了NSLog吗?按照你的建议做了。。。listOfSites的NSLog.count错误:使用未声明的标识符“listOfSites”。我知道我以前做错了什么。。。但我不明白为什么这个类看不到listOfSites…我检查了import slSQLite.h,它就在那里,listOfSites被声明为一个属性。找到了!必须使用“slsqlite”实例限定listOfSites。再次感谢您的大力帮助!我真的,真的很感激!
slSQLite *dbCode = [[slSQLite alloc] init]; // Create an object
[dbCode getListOfSites]; // Generate the content of listOfSites

//  put site data into array for display
slSQLite *item = [[slSQLite alloc] init]; // Create another object
NSLog(@"\n2-The array listOfSites contains %d items", item.listOfSites.count); // Log the length of listOfSites in this new object
slSQLite *dbCode = [[slSQLite alloc] init]; // Create an object
[dbCode getListOfSites]; // Generate the content of listOfSites
NSLog(@"\n2-The array listOfSites contains %d items", dbCode.listOfSites.count); // Log the length of listOfSites in dbCode