StocktotalPostgres.h
#import <Foundation/Foundation.h>
#include "libpq-fe.h"
@interface StocktotalPostgres : NSObject {
const char *conninfo;
PGconn *conn;
}
@end
StocktotalPostgres.m
#import "StocktotalPostgres.h"
@implementation StocktotalPostgres
- (id)init {
self = [super init];
if (self) {
conninfo = "";
conn = PQconnectdb(conninfo);
if (PQstatus(conn) != CONNECTION_OK) {
PQfinish(conn);
}
}
NSLog(@"init StocktotalPostgres instance");
return self;
}
- (void)dealloc {
PQfinish(conn);
NSLog(@"dealloc StocktotalPostgres instance");
}
@end
header file 全都洩,而且 interface 照字面就是 interface,外在,而不是內部。這不是什麼好現象。那就用 Objective-C class extension 藏東西吧!
StocktotalPostgres.h
#import <Foundation/Foundation.h> @interface StocktotalPostgres : NSObject @end
StocktotalPostgres.m
#import "StocktotalPostgres.h"
#include "libpq-fe.h"
@interface StocktotalPostgres()
@property(nonatomic) const char *conninfo;
@property(nonatomic) PGconn *conn;
@end
@implementation StocktotalPostgres
- (id)init {
self = [super init];
if (self) {
self.conninfo = "";
self.conn = PQconnectdb(self.conninfo);
if (PQstatus(self.conn) != CONNECTION_OK) {
PQfinish(self.conn);
}
}
NSLog(@"init StocktotalPostgres instance");
return self;
}
- (void)dealloc {
PQfinish(self.conn);
NSLog(@"dealloc StocktotalPostgres instance");
}
@end
接下來就可以安心寫咚咚嘍。
沒有留言:
張貼留言