LoginSignup
2
1

More than 5 years have passed since last update.

Realm導入

Last updated at Posted at 2018-09-10

Realmとは

CoreDataやSQLiteに変わる次世代Mobileデータベースのこと。

導入手順

CocoaPodsは、Xcodeプロジェクトのライブラリ依存関係を管理するソフトウェア。
これを使って簡単にRealmをインストールする。まず、ターミナルを開き以下のコードを入力する。

ターミナル
sudo gem install cocoapods
pod setup
cd 導入するべきプロジェクトファイルのPATH
pod init

ここまでの操作でプロジェクトファイル内に新しく'Podfile'が作成されている。これに修正を加える。以下は一例である。

Podfile
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'RealmFirstTry' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks! #⬅️⬅️⬅️

  # Pods for RealmFirstTry
  pod 'RealmSwift' #⬅️⬅️⬅️ ソースファイルには import RealmSwift と記述することになる

  target 'RealmFirstTryTests' do
    inherit! :search_paths
    # Pods for testing
  end

  target 'RealmFirstTryUITests' do
    inherit! :search_paths
    # Pods for testing
  end

end

修正後再びターミナルにて以下のコードを打ち込むことで完了する。

ターミナル
pod repo update
pod install

注意点

・.xcodeprojではなく.xcworkspaceを開く。
・プロジェクトファイル内に既にPodfileが存在するとき、pod initはエラーとなる。

参考資料

Qiita SwiftでRealm Databaseを使ってみた: https://qiita.com/ottijp/items/35dee1c03e055e450c81
Qiita【Swift】CocoaPods導入手順 : https://qiita.com/ShinokiRyosei/items/3090290cb72434852460
RealmHP : https://realm.io/docs/swift/latest/
CocoaPodsHP : https://guides.cocoapods.org/using/getting-started.html

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1