0

I am trying to port a project of mine from Android to iOS and I am running into some fundamental issues that have been vexing. Like currently for instance I can't call a method I created because it doesn't seem to exist which is confusing because it is right there.

I'm not sure what I am doing wrong. I have tried looking at some of the books I have on the subject and other questions on this subject and can't find anything that explains what I am doing wrong. As far as I can tell I did it right.

I get the following warnings:

Instance method '-CheckIfTableExists:withDB' not found (return type defaults to 'id')

and

Receiver type 'int' is not 'd' or interface pointer, consider casting it to 'id'

I gather that the second warning is because the compiler thinks it is helping by changing my return value to 'id' instead of 'int' so the variable I am trying to place the value into is mismatching. However I don't understand why it is changing the return type from int to id to begin with and if the return variable is being messed with then the compiler found the method so why is it saying it can not find the method?

Can someone help please? Below is the code in question.

#import "sqlite3.h"

@implementation DatabaseHelper

sqlite3 *db;

NSString *Database_Name = @"UniversalAVDatabase.db";
int Database_Version = 14;

NSString *Database_Table = @"Database";

NSString *Attribute_DB_Version = @"Database_Version";

- (NSString *)filePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:Database_Name];
}

- (int) CheckIfTableExists:(NSString *) TableName withDB:(sqlite3 *) db
{
    NSString *dbQuery = [NSString stringWithFormat:@"SELECT DISTINCT tbl_name FROM sqlite_master WHERE tbl_name = '%@';", TableName];
    sqlite3_stmt *SQLStatement;
    int Result = sqlite3_prepare_v2(db, [dbQuery UTF8String], -1, &SQLStatement, nil);
    return Result;
}

- (void) Initialize
{
    int CurrentDatabaseVersion = 1;
    //attempt to open the database.
    int Result = sqlite3_open([[self filePath] UTF8String], &db);
    if (Result == SQLITE_OK)
    {
        //Check if the Database table exists
        int TableDBExists = 1;
        [TableDBExists CheckIfTableExists:Database_Table withDB:db]; //I get two warnings from this
        if (TableDBExists == 1)
        {
            //If it exists check the version of the database to see if it is a later version.
            //CurrentDatabaseVersion = dbHelper.GetDatabaseVersion(db);
        }
        if (CurrentDatabaseVersion < Database_Version)
        {
            //UpgradeDatabase();
        }
    }
    else
    {
        //If the database does not exist then create it.
    }
}

@end
Daisy
  • 121
  • 11

2 Answers2

1
[TableDBExists CheckIfTableExists:Database_Table withDB:db]; 

Should be:

TableDBExists = [self CheckIfTableExists:Database_Table withDB:db]; 

And you need to include the .h file for your class:

#include "DatabaseHelper.h"

I'm assuming you declared the method you're getting the error in your DatabaseHelper.h file:

- (int) CheckIfTableExists:(NSString *) TableName withDB:(sqlite3 *) db;

There's a bunch of other things I see that tell me you're coming from a different language. See: https://github.com/github/objective-c-conventions and http://blog.gomiso.com/2011/08/15/objective-c-conventions/.

Richard Brown
  • 11,346
  • 4
  • 32
  • 43
  • It should be `TableDBExists = [self CheckIfTableExists:Database_Table withDB:db];`. You forgot `self`. – rmaddy Apr 17 '13 at 04:45
  • Yeah sorry, I missed that line in my code where the DatabaseHelper.h file is imported but it is there. I also have the CHeckIfTableExists method declared in the DatabaseHelper.h file so I should be able to refer to it outside of the class in the rest of the program as needed. Thank you the advice on switching to self worked like a charm. – Daisy Apr 17 '13 at 05:10
0

TableDBExists is of type int in Initialize, you cannot send a message to it.
change it to send the message to self:
TableDBExists = [self CheckIfTableExists:Database_Table withDB:db];

Dan Shelly
  • 5,991
  • 2
  • 22
  • 26