I have a @property listOfSites declared in a class. I am unable to access listOfSites from another class, even tho’ I have done an #import of that class’ .h file. All I did was change an instance of the listOfSites to a property.
This is the code from another class (slTableViewController) where I send message to get 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);
Here is the code for listOfSites:
- (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];
}
To be able to access
listOfSitesfrom other classes, you need to make it a property of that class:In the
MyClass.hfileThen you have to synthesize in the
MyClass.mfileNow, if you import
MyClass.hin another class, then you can access the propertylistOfSites. For example in another fileOtherClass.m:You are making many mistakes in the posted code. (Even assuming you correctly declared
listOfSitesas a property).First, change the line:
into:
Second, in the following lines:
you are generating the content of
listOfSitesfor the objectdbCode, and checking the length oflistOfSitesinitemwhich is a different object and for which you didn’t generate the content oflistOfSites.Try the following instead: