Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // AppDelegate.swift
- // PeopleDB
- //
- // Created by Sabin Tabirca on 08/03/2024.
- //
- import UIKit
- @main
- class AppDelegate: UIResponder, UIApplicationDelegate {
- func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
- // Override point for customization after application launch.
- return true
- }
- // MARK: UISceneSession Lifecycle
- func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
- return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
- }
- func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
- }
- }
- //
- // SceneDelegate.swift
- // PeopleDB
- //
- // Created by Sabin Tabirca on 08/03/2024.
- //
- import UIKit
- class SceneDelegate: UIResponder, UIWindowSceneDelegate {
- var window: UIWindow?
- func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
- guard let _ = (scene as? UIWindowScene) else { return }
- }
- func sceneDidDisconnect(_ scene: UIScene) {
- }
- func sceneDidBecomeActive(_ scene: UIScene) {
- }
- func sceneWillResignActive(_ scene: UIScene) {
- }
- func sceneWillEnterForeground(_ scene: UIScene) {
- }
- func sceneDidEnterBackground(_ scene: UIScene) {
- }
- }
- //
- // PeopleTableViewController.swift
- // PeopleDB
- //
- // Created by Sabin Tabirca on 08/03/2024.
- //https://pastebin.com/5e6Yg4Gd - objective C code
- // https://pastebin.com/7Hj0kcER - swift table view code
- import UIKit
- class PeopleTableViewController: UITableViewController {
- let peopleData = PeopleModel().allPeopleData();
- override func viewDidLoad() {
- super.viewDidLoad()
- }
- // MARK: - Table view data source
- override func numberOfSections(in tableView: UITableView) -> Int {
- // #warning Incomplete implementation, return the number of sections
- return 1
- }
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- // #warning Incomplete implementation, return the number of rows
- return peopleData!.count
- }
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
- // Configure the cell...
- let personData = peopleData![indexPath.row] as! [String]
- cell.textLabel?.text = personData[0]
- cell.detailTextLabel?.text = personData[1]
- return cell
- }
- /*
- // Override to support conditional editing of the table view.
- override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
- // Return false if you do not want the specified item to be editable.
- return true
- }
- */
- /*
- // Override to support editing the table view.
- override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
- if editingStyle == .delete {
- // Delete the row from the data source
- tableView.deleteRows(at: [indexPath], with: .fade)
- } else if editingStyle == .insert {
- // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
- }
- }
- */
- /*
- // Override to support rearranging the table view.
- override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
- }
- */
- /*
- // Override to support conditional rearranging of the table view.
- override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
- // Return false if you do not want the item to be re-orderable.
- return true
- }
- */
- /*
- // MARK: - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation在基于故事板的应用程序中,您通常需要在导航之前做一些准备工作
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
- // Get the new view controller using segue.destination.使用 segue.destination 获取新的视图控制器。
- // Pass the selected object to the new view controller.将选定的对象分配给新的视图控制器。
- }
- */
- }
- //
- // PeopleModel.m
- // PeopleDB
- //
- // Created by Sabin Tabirca on 08/03/2024.
- //
- #import <Foundation/Foundation.h>
- #import "PeopleModel.h"
- @implementation PeopleModel
- static sqlite3 * database;
- // utility methods to where, open, close etc
- -(NSString *)getBundlePath{
- NSString * path = [[NSBundle mainBundle]pathForResource:@"people" ofType:@"sqlite"];
- return path;
- }
- -(NSString *)getDocumentsPath{
- NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString * path = [paths[0] stringByAppendingString:@"/people.sqlite"];
- return path;
- }
- -(void)openDatabase{
- NSString * nsPath = [self getDocumentsPath];
- const char * utfPath = [nsPath UTF8String];
- BOOL success = sqlite3_open(utfPath, &database);
- if(!success){
- NSLog(@"DB CANNOT OPEN");
- }
- }
- -(void)closeDatabase{
- sqlite3_close(database);
- }
- // interface messages
- -(id)init{
- self = [super init];
- // move teh db file from bundle to documents将 db 文件从捆绑包移动到文档
- NSString * bundlePath = [self getBundlePath];
- NSString * documentPath = [self getDocumentsPath];
- NSFileManager * manager = [NSFileManager defaultManager];
- if(![manager fileExistsAtPath:documentPath]){
- [manager copyItemAtPath:bundlePath toPath:documentPath error:NULL];
- }
- return self;
- }
- -(NSArray *)allPeopleData{
- NSMutableArray * data = [[NSMutableArray alloc]initWithCapacity:20];
- [self openDatabase];
- // do retrieve work做检索工作
- const char * query = "select * from people";
- static sqlite3_stmt * statement;
- BOOL success = sqlite3_prepare_v2(database, query, -1, &statement, NULL);
- if(!success){
- NSLog(@"DB CANNOT MAKE STATEMENT");
- }
- while(sqlite3_step(statement) == SQLITE_ROW){
- // extract the data from the statement从语句中提取数据
- const char * name = (const char *)sqlite3_column_text(statement, 0);
- const char * phone = (const char *)sqlite3_column_text(statement, 1);
- // place name and phone to data地名和电话到数据
- NSString * nsName = [NSString stringWithUTF8String:name];
- NSString * nsPhone = [NSString stringWithUTF8String:phone];
- [data addObject:@[nsName,nsPhone]];
- }
- [self closeDatabase];
- return data;
- }
- @end
- //https://pastebin.com/3Z2PPZqY
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement