Swiftの覚書1
公開日:
:
最終更新日:2014/08/09
iPhone App 開発, Swift iPhone App, Swift
Warning: Use of undefined constant user_level - assumed 'user_level' (this will throw an Error in a future version of PHP) in /home/users/1/juny/web/hidef/wp-content/plugins/ultimate-google-analytics/ultimate_ga.php on line 524
Swift
先日、Appleから新言語Swiftが発表されました。
今までiOSアプリで開発していたObjective-Cから移行しそうな勢いなので、XcodeのBeta版をインストールして勉強しています。
Xcode Beta版やiOS8ダウンロードはこちらのページから。
https://developer.apple.com/devcenter/ios/index.action
Swiftに関するドキュメントはiBooksからも公開されています。
https://itunes.apple.com/us/book/swift-programming-language/id881256329?mt=11
以下、テストプログラムを作成中のときに気づいたことを覚書に。
遅延初期化
次のコードはCoreDataのsqliteファイルを読み込む処理です。
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent(“MasterDetailTest.sqlite”)
// 以下略
return coordinator
}()
lazyとは遅延プロパティ(インスタンスを生成した際は処理されずに変数アクセス時にコールされる)とのこと。
A lazy stored property is a property whose initial value is not calculated until the first time it is used. You indicate a lazy stored property by writing the
lazy
modifier before its declaration.
Objective-Cでは
if (_persistentStoreCoordinator != nil) {
return_persistentStoreCoordinator;
}
という記述が必要でしたが、変数のアクセス時にnilチェックがいらないってことですごく便利です。
sqliteの格納場所
CoreDataを使ったアプリ作成の際に、いままでの格納フォルダが変更されていました。
正式版リリースでは元に戻るのかな?
以前の場所
Users/ユーザー/Library/Application Support/iPhone Simulator/iOSのバージョン/Applications/ランダムな識別子/Documents/
変更後
Users/ユーザー/Library/Developer/CoreSimulator/Devices/ランダムな識別子/data/Containers/Data/Application/ランダムな識別子/Documents/
以下の関数でアプリケーションURLが取得できます。
[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]
また何かあれば追記していきます。
ad
関連記事
-
-
[Objective-C] バージョン更新時に変更内容をアラートで出力する
アプリの更新内容を表示したい! バージョン更新時に変更内容を表示させたいのは、ユーザーがいちいちA
-
-
[Swift] CoreDataで検索したデータを削除する
CoreDataの検索と削除 前の投稿 CoreDataを使ってみる をふまえて今度は検索&削除
-
-
[Objective-C] 文字列操作の便利Tips
Tipsいろいろ 先日の投稿 共通関数をまとめたクラスを作ろう!の流れで、文字列を操作するときに便
-
-
[Swift] 関数型プログラミング
関数型プログラミングについて、ストーリー仕立ての面白い記事がありましたので紹介します。 IQ1
-
-
[iPhone App] WorkManager 1.2.0版がリリースされました。
新機能 以前から要望などが多かったものを元に、機能を追加いたしました。 新バージョンで追加された
-
-
[Swift] スクリーンサイズを取得する
スクリーンサイズを取得 iPhoneの幅と高さを取得する関数を紹介します。 iOS8から画面の傾
-
-
[Swift] ビルド時のエラー&iOS8でNSBigDecimalのバグ
ビルド時のエラー Swiftプロジェクトを実行中に以下のエラーが出ました。 "__TFSs15_
-
-
[Swift] frameworkについて
先日、Swiftで実装されたライブラリをまとめたサイトを発見しました。 Swift Toolb
-
-
[Swift] UITabBarControllerでタブ選択時のイベントを取得する
UITabBarController タブを使った画面を作成する場合はUITabBarContro