アイフォンアプリ開発、swif3.0 新しいDB Realmをインストールしてみた
SQLiteが主流かと思ったら、新しいDB Realmが便利そうだということで、インストールしてみた
SQLiteがいいのかと思っていたら、どうも新しいDBに移行し始めているらしい。
新しいものは使いにくことがあるけれど、廃れてしまって使われない技術も困るので、新しいDBを使ってみることに。
インストール方法はZIPファイルをダウンロードする方法と podを使う方法があるらしい。
podを使ってみることにしました。
<結果>
うまくいったようで、でもうまくいっていないようです。
ld: warning: directory not found for option ‘-F/Users/maseda/Documents/新書類/xcode/2016/spaceEitango1121/build/Debug-iphoneos/Realm’
ld: warning: directory not found for option ‘-F/Users/maseda/Documents/新書類/xcode/2016/spaceEitango1121/build/Debug-iphoneos/RealmSwift’
というエラーが出てしまい、新規ファイルでRealmのファイル雛形が出ていません。
もう一度プロジェクトファイルを作ってやってみます。
その後、ZIPファイルをダウンロードして、読み込んでOKでした。ちなみに雛形はありません。
まとめると、今のところPODをインストールして、うまくいっていないようなら、ZIPからもフレームワークを読み込むです。
そして、
一度コンパイルします。
これで、次回から import RealmSwiftでエラーがでなくなるでしょう。
その後、プラグインをインストールします。これは、ZIPでダウンロードしたファイルを展開するとPluginフォルダがあるので、その中のプロジェクトファイルをビルドして、XCODEを再起動すると新規ファイルで、Realmの雛形ファイルが選べます。このプラグインでRealmクラスを作らないとサンプルのコードがエラーになります。
(1)
これが意外にも面倒。
podを使うらしいが私のマックに入っていない。そこでインストールから
aidayota-no-MacBook-Pro:~ maseda$ pod --version -bash: pod: command not found aidayota-no-MacBook-Pro:~ maseda$ sudo gem install cocoapods Password: Fetching: i18n-0.7.0.gem (100%) Successfully installed i18n-0.7.0
(以下にインストールが進む)
(2)
aidayota-no-MacBook-Pro:~ maseda$ pod –version
1.1.1
aidayota-no-MacBook-Pro:~ maseda$ pod repo update
(3)
XCODEプロジェクトファイルのフォルダに移動
aidayota-no-MacBook-Pro:2016 maseda$ cd spaceEitango1121/
(3-1)
swiftバージョンチェック
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -v Welcome to Apple Swift version 3.0.1 (swiftlang-800.0.58.6 clang-800.0.42.1). Type :help for assistance.
と出力される
Swiftバージョンをメモっておく
(4)
pod initでpodファイルが作成される
aidayota-no-MacBook-Pro:spaceEitango1121 maseda$ pod init aidayota-no-MacBook-Pro:spaceEitango1121 maseda$ vi Podfile
ここのファイルに、
use_frameworks!
pod ‘RealmSwift’
を記述する。
またはバージョンを指定するときは
「pod ‘RealmSwift’, ‘~> 2.4’」
みたいにバージョンを指定する。するとバージョンで2.4.1みたいに2.4以降のバージョンが有るとインストールされるらしい
>>実際やってみると、
次のようなエラーになって、2.4.1はインストールされず。仕方ないので、バージョンをしてせずに、pod installを実行すると2.1.1がインストールされた。その後、Projectファイルには2.4.1のバージョンをドラッグして取り込んだ。
果たして2.1.1で作成したProjectファイルと2.4.1のバージョンとでコンフリクトが起きないかどうか?
—
Swiftのバージョンを記述する
そして、
公式サイトの内容をPodfileの最後に記載する
post_install do |installer| installer.pods_project.targets.each do |target| target.build_configurations.each do |config| config.build_settings['SWIFT_VERSION'] = '3.0' end end end
(5)
そして、
aidayota-no-MacBook-Pro:spaceEitango1121 maseda$ pod install
でインストールが始まる。10分位かかる
実はプロジェクトファイルを開いていたことをインストール後に思い出しました。次回からは閉じてからインストールしようかと。
aidayota-no-MacBook-Pro:spaceEitango1121 maseda$ pod install Setting up CocoaPods master repo CocoaPods 1.2.0.beta.1 is available. To update use: `sudo gem install cocoapods --pre` [!] This is a test version we'd love you to try. For more information, see https://blog.cocoapods.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.2.0.beta.1 Setup completed Analyzing dependencies Downloading dependencies Installing Realm (2.1.1) Installing RealmSwift (2.1.1) Generating Pods project Integrating client project [!] Please close any current Xcode sessions and use `spaceEitango1121.xcworkspace` for this project from now on. Sending stats Pod installation complete! There is 1 dependency from the Podfile and 2 total pods installed. [!] The `spaceEitango1121Tests [Debug]` target overrides the `ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES` build setting defined in `Pods/Target Support Files/Pods-spaceEitango1121Tests/Pods-spaceEitango1121Tests.debug.xcconfig'. This can lead to problems with the CocoaPods installation - Use the `$(inherited)` flag, or - Remove the build settings from the target. [!] The `spaceEitango1121Tests [Release]` target overrides the `ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES` build setting defined in `Pods/Target Support Files/Pods-spaceEitango1121Tests/Pods-spaceEitango1121Tests.release.xcconfig'. This can lead to problems with the CocoaPods installation - Use the `$(inherited)` flag, or - Remove the build settings from the target. [!] The `spaceEitango1121UITests [Debug]` target overrides the `ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES` build setting defined in `Pods/Target Support Files/Pods-spaceEitango1121UITests/Pods-spaceEitango1121UITests.debug.xcconfig'. This can lead to problems with the CocoaPods installation - Use the `$(inherited)` flag, or - Remove the build settings from the target. [!] The `spaceEitango1121UITests [Release]` target overrides the `ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES` build setting defined in `Pods/Target Support Files/Pods-spaceEitango1121UITests/Pods-spaceEitango1121UITests.release.xcconfig'. This can lead to problems with the CocoaPods installation - Use the `$(inherited)` flag, or - Remove the build settings from the target. aidayota-no-MacBook-Pro:spaceEitango1121 maseda$
次に
(6)XCODEのプロジェクトフォルダを見ると新しく「spaceEitango1121.xcworkspace」というのが作成されている。拡張子前はプロジェクトファイル名。
このプロジェクトファイルを起動する。
すると
Podsプロジェクトが見える。このプロジェクトではなくて、
いままで作成していたプロジェクトを選択し、
Linked Frameworks and Libraries」を選択してFrameworkを追加
RealmのFrameworkを2つ追加します。
これでインストールはOKみたい
しかし、このままでは、使えません。
一度、RealmSwiftのZIPファイルもダウンロードしておきます。
そして、/Users/maseda/Downloads/realm-swift-2.1.1のなかにiOS用のSwiftのバージョンのRealmのフレームワーク2つがあるので、それをspaceEitango1121(ここは個別のプロジェクト名).xcworkspaceプロジェクトに読み込みます。
アイコンがケースの形をしていたらOK。
そして、
EmbetBinariesのところにもRealmのフレームワーク2つを呼び込みます。
これで
import RealmSwiftと記述してエラーが出なければOKかも。
もしエラーなら
一度コンパイルします。
これで、次回から import RealmSwiftでエラーがでなくなるでしょう。
そして
BuildPhaseでNew Run Scriptで追加して
bash "${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH}/Realm.framework/strip-frameworks.sh"
Realm ブラウザーを使うときに
「Cmd + shift + g」
を使うとパスが入力できる。
設定だけの動画です
その後の、Realmのプロジェクトへのインストール方法
pod initでpodファイルが作成される
aidayota-no-MacBook-Pro:spaceEitango1121 maseda$ pod init aidayota-no-MacBook-Pro:spaceEitango1121 maseda$ vi Podfile
ここのファイルに、
use_frameworks!
pod ‘RealmSwift’
を記述する。
またはバージョンを指定するときは
「pod ‘RealmSwift’, ‘~> 2.4’」
みたいにバージョンを指定する。するとバージョンで2.4.1みたいに2.4以降のバージョンが有るとインストールされるらしい
>>実際やってみると、
次のようなエラーになって、2.4.1はインストールされず。仕方ないので、バージョンをしてせずに、pod installを実行すると2.1.1がインストールされた。その後、Projectファイルには2.4.1のバージョンをドラッグして取り込んだ。
果たして2.1.1で作成したProjectファイルと2.4.1のバージョンとでコンフリクトが起きないかどうか?
[!] Unable to satisfy the following requirements: - `RealmSwift (>= 2.4.1)` required by `Podfile` None of your spec sources contain a spec satisfying the dependency: `RealmSwift (>= 2.4.1)`. You have either: * out-of-date source repos which you can update with `pod repo update`. * mistyped the name or version. * not added the source repo that hosts the Podspec to your Podfile. Note: as of CocoaPods 1.0, `pod repo update` does not happen on `pod install` by default.
どうやらpodのバージョンが古いからだそうです。
pod repo update
でアップデートして
sudo gem install cocoapods
で実行
aidayota-no-MacBook-Pro:KihonJouho01 maseda$ sudo gem install cocoapods Password:>>自分のパスワード Fetching: cocoapods-core-1.2.0.gem (100%) Successfully installed cocoapods-core-1.2.0 Fetching: cocoapods-downloader-1.1.3.gem (100%) Successfully installed cocoapods-downloader-1.1.3 Fetching: cocoapods-trunk-1.1.2.gem (100%) Successfully installed cocoapods-trunk-1.1.2 Fetching: molinillo-0.5.5.gem (100%) Successfully installed molinillo-0.5.5 Fetching: ruby-macho-0.2.6.gem (100%) Successfully installed ruby-macho-0.2.6 Fetching: cocoapods-1.2.0.gem (100%) Successfully installed cocoapods-1.2.0 Parsing documentation for cocoapods-core-1.2.0 Installing ri documentation for cocoapods-core-1.2.0 Parsing documentation for cocoapods-downloader-1.1.3 Installing ri documentation for cocoapods-downloader-1.1.3 Parsing documentation for cocoapods-trunk-1.1.2 Installing ri documentation for cocoapods-trunk-1.1.2 Parsing documentation for molinillo-0.5.5 Installing ri documentation for molinillo-0.5.5 Parsing documentation for ruby-macho-0.2.6 Installing ri documentation for ruby-macho-0.2.6 Parsing documentation for cocoapods-1.2.0 Installing ri documentation for cocoapods-1.2.0 6 gems installed
そして、再度pod install
しました。
まだ2.4.1がインストールできるかは試していません。