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
関連記事
-
-
iPhoneApp WorkManager公開されました!
きっとリジェクトされるんだろうなと思いながら申請を出して1週間。 本日、公開のメールが届きまし
-
-
[Swift] プロパティリスト(plist)を使ってCoreDataで一括登録する
プロパティリストを使う 初期データやあらかじめ用意したデータを一括登録したい場合、プロパティリスト
-
-
[Swift] プロパティリスト(plist)の値を取得
plistからデータを取得してみます。 こちらのエントリーも参考にしてみてください。 プロパテ
-
-
[Swift] Asset Catalogについて
XCode5から追加されたAsset Catalog。 いままで標準のImages.xcasset
-
-
OSSの使い方がわからない・・・。
チュートリアル系の、storyboradでview controllerを置いて画面遷移を設定してっ
-
-
[Objective-C] no architectures to compile for … のコンパイルエラーについて
起動したプロジェクトでコンパイルエラーが出ました。 no architectures to com
-
-
小さなことからコツコツと
これまでHTMLとCSSだけで生きてきただけあって、プログラム言語がいまいちわからない。 Mo
-
-
[iPhone App] WorkManagerの不具合について
バージョン1.2.2の不具合 11/25日現在、iPhoneアプリの「WorkManger」バージ
-
-
TabBarControllerとNavigationControllerのボタン追加
TabBarとNavigationを同時に使っているViewControllerの場合、naviga