1

大家好,我知道类方法和实例方法之间的区别。

我的问题是“当一个类方法返回任何值/任何东西时,返回的值可以保存在变量/任何对象中吗?” 因为在我的代码类方法中,将 SQLite 数据库中存在的行数从 SQLManger 类返回给 ViewController,但在 viewController 中,变量“noOFRows”没有填充类方法返回的任何值。

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;
}

来自 ViewController 的消息调用在这里

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;
}

SQLManager 类方法中的变量“noOfRows”包含一些值(即:7),但是当调用 return 语句时,这些值不会反映回 ViewController.m 的实例变量“noOfRows”

请避免任何类型的错误。

谢谢 。

4

0 回答 0