Pregunta

I am fairly new at using xCode, so sorry if this question seems strange.

In my app I would like all the views and tableviews to channel their DB (sqlite3) access trough one object. I already got the openDatabase function running but seem to have an issue with the function that creates a new one when none is available.

It all used to work when the code was in the view itself, but centralising it made it a lot more difficult than I had anticipated :(.

below my code for the .h and .m:

beDbAccess.h

//
//  beDbAccess.h
//  myDiveApp
//
//  Created by Jurgen on 07/09/13.
//  Copyright (c) 2013 Dictus. All rights reserved.
//

#import "beObject.h"
#import "sqlite3.h"

@interface beDbAccess : NSObject
{
    NSArray *path;
    NSString *docPath;
    NSString *dbPathString;
    NSFileManager *fileManager;
}

@property (nonatomic, retain) NSArray *path;
@property (nonatomic) NSString *docPath;
@property (nonatomic) NSString *dbPathString;
@property (nonatomic) NSFileManager *fileManager;

#pragma functions callable from the outside
NSString *openDatabase();

#pragma functions
-(void)createNewDatabase;

@end

=================

beDbAccess.m

//
//  beDbAccess.m
//  myDiveApp
//
//  Created by Lion on 07/09/13.
//  Copyright (c) 2013 Dictus. All rights reserved.
//
//  LET EROP DAT JE NOOIT '' gebruikt rond de veldnamen in de WHERE CLAUSE !!!

#import "beDbAccess.h"

@implementation beDbAccess
@synthesize path, dbPathString, docPath, fileManager;

NSMutableArray *arrayOfButtons;

sqlite3 *myDb;
NSArray *path;
NSFileManager *fileManager;
NSString *docPath, *dbPathString;
UIRefreshControl *refreshControl;

NSString *openDatabase()
{
    sqlite3 *myDb;
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docPath = [path objectAtIndex:0];
    NSString *dbPathString = [docPath stringByAppendingPathComponent:@"myDb.db"];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:dbPathString])
    {
        const char *dbPath = [dbPathString UTF8String];
        if (sqlite3_open(dbPath, &myDb)==SQLITE_OK)
        {
            NSLog(@"   Create Database: EMPTY ...");
 /*!*/      [self createNewDatabase]; // This is where the problem lies... Use of undeclared identifier 'self' :(
        } else NSLog(@"#Local: db open...");
    }
    return dbPathString;
}
¿Fue útil?

Solución

Functions are not methods conceptually

Functions don't know about a self pointer because they typically don't belong to a class


In your case you have to make the function part of the class OR give it access to the class some other way (which wouldn't be OO style)

e.g.

@interface beDbAccess : NSObject
{
    NSArray *path;
    NSString *docPath;
    NSString *dbPathString;
    NSFileManager *fileManager;
}

@property (nonatomic, retain) NSArray *path;
@property (nonatomic) NSString *docPath;
@property (nonatomic) NSString *dbPathString;
@property (nonatomic) NSFileManager *fileManager;

#pragma functions callable from the outside
+ (id)sharedInstance;
-(NSString*)openDatabase;

#pragma functions
-(void)createNewDatabase;

@end

THEN use it from the outside like:

[[beDbAccess sharedInstance] openDatabase];
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top