3 Replies - 2053 Views - Last Post: 16 August 2011 - 12:58 PM

#1 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3030
  • View blog
  • Posts: 10,558
  • Joined: 08-August 08

Objective C class implementation

Posted 15 August 2011 - 12:02 PM

My code works as it is, but I'm getting a warning: Incomplete implementation of class 'MagnoliaData'
I'm thinking I'm missing setters/getters, but that's what I'm really having trouble with!

I'd like to be able to set id_num, but can't see how to call a method to do that. I've tried:
    int example = 2;
    [MagnoliaData database setId_num:example];


and variations of:
    [[[MagnoliaData] database] setId_num:example];




Magnoliadata.h
#import <Foundation/Foundation.h>
#import <sqlite3.h>

@interface MagnoliaData : NSObject {
    sqlite3 *_database;
}

+ (MagnoliaData*)database;
- (NSArray *)MagnoliaInfos;
- (NSString *) getWritableDBPath;
- (NSString *)createEditableCopyOfDatabaseIfNeeded;
- (NSArray *)ShowItemNumber;

@end


Magnoliadata.m
#import "MagnoliaInfo.h"
#import "MagnoliaData.h"

#define DATABASE_NAME @"Magnolia.db"
#define DATABASE_TYPE @"sqlite3"

@implementation MagnoliaData

static MagnoliaData *_database;
int id_num = 2;

+(MagnoliaData*)database {
    if (_database == nil) {
        _database = [[MagnoliaData alloc] init];
    }
    return _database;
}

- (id)init {
    if ((self = [super init])) {
        NSString *sqLiteDb = [self createEditableCopyOfDatabaseIfNeeded ];
        if (sqlite3_open([sqLiteDb UTF8String], &_database) != SQLITE_OK) {
            NSLog(@"Failed to open database!");
        }
    }
    return self;
}

- (void)dealloc {
    sqlite3_close(_database);
    [super dealloc];
}


- (NSArray *)ShowItemNumber {
    NSString *sqlStatement = @"SELECT id, item_type, description, date_issued, completed_expired FROM magnolia_one WHERE id=?";  
    NSLog(@"%@",sqlStatement);
        // NSLog(@"%@",id_num);
    sqlite3_stmt *statement;
    NSMutableArray *retval = [[[NSMutableArray alloc] init] autorelease];   
    int errr;
        //*
    if ((errr = sqlite3_prepare_v2(_database, [sqlStatement UTF8String], -1, &statement, nil)) == SQLITE_OK)  {
        sqlite3_bind_int( statement, 1, id_num);			
        NSLog(@"success!");
        while ((errr = sqlite3_step(statement)) == SQLITE_ROW) {
            char *item_type = (char *) sqlite3_column_text(statement, 1);
            char *desc = (char *) sqlite3_column_text(statement, 2);
            char *date_issued = (char *) sqlite3_column_text(statement, 3);
            char *comp = (char *) sqlite3_column_text(statement, 4);
            
            if (item_type != nil) {
                NSString *description = [[NSString alloc] initWithUTF8String:desc];
                NSString *mytype = [[NSString alloc] initWithUTF8String:item_type];
                NSString *issued_date = [[NSString alloc] initWithUTF8String:date_issued];
                NSString *completed_expired = [[NSString alloc] initWithUTF8String:comp];
                NSLog(@"Desc: %@",description);
                NSLog(@"Type: %@",mytype);
                NSLog(@"Issued: %@",issued_date);
                NSLog(@"Comp: %@",completed_expired);
                MagnoliaInfo *info = [[MagnoliaInfo alloc]initWithUniqueId:(int)id_num mytype:(NSString *)mytype description:(NSString *)description date_issued:(NSString *)date_issued completed_expired:(NSString *)completed_expired];
                [retval addObject:info];
                [description release];
                [mytype release];
                [issued_date release];
                [completed_expired release];
                [info release];
            } else { 
                NSLog(@"Did not read data.");
            }
        }
        NSLog(@"End of while loop");
    }
    return retval;
        //*/
}

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

-(NSString *)createEditableCopyOfDatabaseIfNeeded 
{
        // Testing for existence
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
														 NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:DATABASE_NAME];
	
    success = [fileManager fileExistsAtPath:writableDBPath];
    if (success)
        return writableDBPath;
	NSLog(@"copying file...");
        // The writable database does not exist, so copy the default to
        // the appropriate location.
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath]
							   stringByAppendingPathComponent:DATABASE_NAME];
    success = [fileManager copyItemAtPath:defaultDBPath
								   toPath:writableDBPath
									error:&error];
    if(!success)
    {
        NSAssert1(0,@"Failed to create writable database file with Message : '%@'.",
				  [error localizedDescription]);
    }
    return writableDBPath;
}

@end


I'm using XCode 4.02

Is This A Good Question/Topic? 0
  • +

Replies To: Objective C class implementation

#2 GWatt  Icon User is offline

  • member icon

Reputation: 278
  • View blog
  • Posts: 3,078
  • Joined: 01-December 05

Re: Objective C class implementation

Posted 15 August 2011 - 01:06 PM

The correct way to do the call is this:

[[MagnoliaData database] setId_num: example];

Also, it doesn't appear that you have implemented a setId_num selector or even have an appropriate field that such a selector would modify.
Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3030
  • View blog
  • Posts: 10,558
  • Joined: 08-August 08

Re: Objective C class implementation

Posted 16 August 2011 - 09:41 AM

Thanks. I had tried calling it that way, but it didn't work because I hadn't set the field properly. Coming from PHP and C++ I'd assumed that this:
@implementation MagnoliaData

static MagnoliaData *_database;
int id_num = 2;
// etc...


did that, but I was wrong. For anyone interested, It needs to be set in the interface:
@interface MagnoliaData : NSObject {
    sqlite3 *_database;
    int id_num;
}


Was This Post Helpful? 0
  • +
  • -

#4 GWatt  Icon User is offline

  • member icon

Reputation: 278
  • View blog
  • Posts: 3,078
  • Joined: 01-December 05

Re: Objective C class implementation

Posted 16 August 2011 - 12:58 PM

You can implement the setter (and getter if you want) like this:
// MagnoliaData.h
@interface
/*
 stuff
*/
@end

@property (writeonly) int setId_num;
//@property (readwrite) int setId_num;



// MagnoliaData.m

#import "MagnoliaData.h"

@synthesize setId_num;

@implemntation
/*
 stuff
*/
@end


This post has been edited by GWatt: 16 August 2011 - 12:58 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1