hi everyone I know the difference between class method and instance method.
My question is "when a class methods returns any value/anything, that returned value can be hold in variable/any object ?" because in my code class method is returning the number of rows present in SQLite database from a SQLManger class to ViewController, but in viewController the variable "noOFRows" is not filled with any values which is returned by the class method.
SQLManager.m
+(int)noOFRowsInDatabases:(NSString*)_databasePath quizDatabase:(sqlite3*)_quizDatabase
{
NSString *documentDirectory;
NSArray *diretoryPath;
diretoryPath=NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
documentDirectory=diretoryPath[0];
// creates the database path by appending the databases name as string
_databasePath=[[NSString alloc]initWithString:[documentDirectory stringByAppendingString:@"quizdatabase.db"]];
NSFileManager *fileManager=[NSFileManager defaultManager]; // responsible for creating the file manager object which helps in creating the path
if([fileManager fileExistsAtPath:_databasePath]==YES)
{
const char *dbPath=[_databasePath UTF8String];
if(sqlite3_open(dbPath, &_quizDatabase)==SQLITE_OK) // Opens the databases by provding the path for the database
{
sqlite3_stmt *statement; // Prepared statement object
NSString *countString=[NSString stringWithFormat:@"SELECT count(*) FROM QUIZ"];
const char *countQuery=[countString UTF8String];
if(sqlite3_prepare_v2(_quizDatabase, countQuery, -1, &statement, NULL)==SQLITE_OK) // Prepares the statment object -1 to prepare the sql statment in till the null charecter encountered
{
if(sqlite3_step(statement)) // Evaluates the prepared Statement
{
int noOfRows=[[[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)] intValue];
NSLog(@"%d",noOfRows);
sqlite3_finalize(statement); // Destroyes the prepared statemnt object
sqlite3_close(_quizDatabase); // closes the database connection
return noOfRows;
}
}
}
}
return -1;
}
Message call from ViewController is here
noOfRows=[SQLManager noOFRowsInDatabases:_databasePath quizDatabase:_quizDatabase]; // Calls the message to retrieve the total number of rows present in databases
if(noOfRows<0)
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"Return -1" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
return;
}
The variable "noOfRows" in SQLManager Class methods holds some values (ie: 7), but when return statement is called the values is not reflecting back into the instance variable "noOfRows" of ViewController.m
Please avoid any kind of mistakes.
Thanks .