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
関連記事
-
[Swift] CoreDataを使ってみる
SwiftでCoreData 今回はSwiftでCoreDataを使ってみます。 Xcodeプロ
-
[Swift] Asset Catalogについて
XCode5から追加されたAsset Catalog。 いままで標準のImages.xcasset
-
[Swift] Swiftでいろんなfor文まとめてみた
いろんなfor文 プログラム作成で必ず出てくるfor文を自分なりにまとめてみました。 Swift
-
[Swift] 定数クラスをつくってみる
Objective-cの場合 今回は共通にする定数をまとめるクラスを作ってみます。 まずはO
-
iPhoneApp WorkManager公開されました!
きっとリジェクトされるんだろうなと思いながら申請を出して1週間。 本日、公開のメールが届きまし
-
[Objective-C] 小数点を丸める!(数値変換)
小数点を含む数値 Objective-Cに限らず、小数点を含む数値を計算する場合はdoubleやf
-
[Objective-C] 文字列操作の便利Tips
Tipsいろいろ 先日の投稿 共通関数をまとめたクラスを作ろう!の流れで、文字列を操作するときに便
-
[Swift] optional値の設定(??)
optional値がnilの時 プロジェクトでのテンプレートを選択した時にソースコードが自動生成さ