大家好,我知道类方法和实例方法之间的区别。
我的问题是"当类方法返回任何值/任何值时,返回的值可以保存在变量/任何对象中?"因为在我的代码类中,方法是将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;
}
变量&#34; noOfRows&#34;在SQLManager中,Class方法包含一些值(即:7),但是当调用return语句时,值不会反映回实例变量&#34; noOfRows&#34; of ViewController.m
请避免任何错误。
谢谢。