1

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 .

Nasir
  • 1,617
  • 2
  • 19
  • 34
  • 1
    Sorry, but have you actually debugged your `noOFRowsInDatabases` method to make sure it returns the desired output? There's no way it becomes something different when the returned value is assigned to some local variable. – Mikayil Abdullayev Dec 15 '14 at 07:40
  • yeah , I debugged. I places the breakpoints over the message call and and method definition and checked the value in the local variable of SQLManager class method, using PO cmd in console, i prints the 7 (ie: no of rows present in SQLite database). – Nasir Dec 15 '14 at 08:46
  • You know, it's a weird situation. Can I just ask you to replace the contents of the `noOFRowsInDatabases ` method with just `return 1;` and see what happens. – Mikayil Abdullayev Dec 15 '14 at 10:23
  • thanks , i fixed that problem, Thanks again – Nasir Dec 15 '14 at 13:03
  • What was the problem? – Mikayil Abdullayev Dec 15 '14 at 19:48
  • Next to the message calling for class method, I dealing with the instance method, but without initialising the the instance of class I was calling the other method, so that was the problem. The problem is solved, and I came to conclusion that class method returns the same thing as instance method. Correct me of you disagree with this conclusion. Thank You Once again – Nasir Dec 18 '14 at 04:49

0 Answers0