10月 282017
Swift SharkORM を使用するには?
新規プロジェクトを XCode で作成して、一旦閉じる。(SharkORMTest)
ターミンナルより SharkORMTest に移動
pod init
Profile に 以下の様に編集
# Uncomment the next line to define a global platform for your project platform :ios, '11.0' target 'SharkOrmTest' do # Comment the next line if you're not using Swift and don't want to use dynamic frameworks #use_frameworks! pod 'SharkORM' # Pods for SharkOrmTest target 'SharkOrmTestTests' do inherit! :search_paths # Pods for testing end target 'SharkOrmTestUITests' do inherit! :search_paths # Pods for testing end end以下のコマンドを実行
pod installxcode にて SharkOrmTest.xcodeproj ではなく SharkOrmTest.xcworkspace を開く
SharkHeader.h ブリッジヘッダーファイルを作成
#ifndef SharkHeader_h #define SharkHeader_h #include <SharkORM/SharkORM.h> #endif /* SharkHeader_h */build settings
-> objective-c Bridging Heatter に以下を追加$(SRCROOT)/$(PRODUCT)/SharkHeader.h上記を忘れると以下のエラーが出ます。
Use of undeclared type 'SRKDelegate' Use of undeclared type 'SRKObject'テーブルに対応するクラスを作成
import Foundation import SharkORM class Sentence: SRKObject { @objc dynamic var _id : NSNumber? @objc dynamic var categoryId : NSNumber? @objc dynamic var english : String? @objc dynamic var japanese : String? @objc dynamic var okCount : NSNumber? @objc dynamic var ngCount : NSNumber? @objc dynamic var average : NSNumber? }以下、追加
import SharkORM class AppDelegate: UIResponder, UIApplicationDelegate, SRKDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. SharkORM.setDelegate(self) SharkORM.openDatabaseNamed("myDatabase") initDb() return true } func initDb() { insertData(english:"this is a pen.") insertData(english:"this is an apple.") insertData(english:"can you speack japanese.") var results = Sentence.query().fetch() print(String(format: "count:%d", (results?.count)!)) for sentence in results! { let s = sentence as! Sentence print((sentence as AnyObject).english) } } func insertData( english: String ) { var a = Sentence() a.english = english a.commit() }色々とハマタ