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

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] optional値の設定(??)

公開日: : 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

optional値がnilの時

プロジェクトで[Master Detail Application]のテンプレートを選択した時にソースコードが自動生成されますが、見慣れない記述があったのでメモ。

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return self.fetchedResultsController.sections?.count ?? 0
}

この関数はUITableViewDelegateのDelegateメソッドで、テーブルビューのセクション数を返します。
問題は??の部分。
調べてみると、??の左側のオブジェクト(optional値)がnilの場合は右側のオブジェクトを返す記述らしいです。

以下、wikiから抜粋
Swift (プログラミング言語)

左オペランドにはT?型、右オペランドにはT型の値をとり、
左オペランドに値が存在していればアンラップしてその値を返し、左オペランドがnilであれば右オペランドの値を返す

"x".toInt() ?? 0  // 0
"5".toInt() ?? 0  // 5

なるほど。
三項演算子と似ているようで用途は全然違いますね。

ad

関連記事

no image

OSSの使い方がわからない・・・。

チュートリアル系の、storyboradでview controllerを置いて画面遷移を設定してっ

記事を読む

[Objective-C] デリゲート (Delegate)を理解する!

デリゲートとは? デリゲートに関しての意味合いとして、”委譲”とか”代理”と説明されることが多いで

記事を読む

[Swift] CoreDataのテーブルからデータ件数を取得する

件数を取得する fetchResultsControllerなどを使わず、直接テーブルのデータ件数

記事を読む

[iPhone App]WorkManager アップデートしました。

Processing For App Storeでやきもきした僕です。こんにちわ。 今

記事を読む

[Objective-C] no architectures to compile for … のコンパイルエラーについて

起動したプロジェクトでコンパイルエラーが出ました。 no architectures to com

記事を読む

備忘録的メモ

Xcode5にアップデートして、とりあえずアプリを走らせてみたらナビゲーションバーがかぶってる・・・

記事を読む

[Swift] 画像を合成する

画像を合成 以下の2つの画像を合成してみます。円と四角です。 画像をプロジェクトに追

記事を読む

[Objective-C] 文字列操作の便利Tips

Tipsいろいろ 先日の投稿 共通関数をまとめたクラスを作ろう!の流れで、文字列を操作するときに便

記事を読む

[Swift] Asset Catalogについて

XCode5から追加されたAsset Catalog。 いままで標準のImages.xcasset

記事を読む

[Swift] プロパティリスト(plist)を使ってCoreDataで一括登録する

プロパティリストを使う 初期データやあらかじめ用意したデータを一括登録したい場合、プロパティリスト

記事を読む

ad

Message

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

ad

[PHP] curl転送してみる(googleに)

curlでgoogle画像検索APIにアクセスしてみます。 cu

[PHP] PDOでMySQLの接続確認をする

PDO PHPでMySQLに接続する際には、mysql_connec

[PHP] ランダムな英数字を生成する

便利系メソッド 今回はPHPでランダムな英数字を作成してみます。

[Swift] プロパティリスト(plist)の値を取得

plistからデータを取得してみます。 こちらのエントリーも参考にし

[Swift] Asset Catalogについて

XCode5から追加されたAsset Catalog。 いままで標準

→もっと見る

  • 1978年の七夕生まれ。 25才でweb業界の門を叩き、28才でフリーランスに。 現在は、フリーランスでマークアップ中心に、wordpressのカスタマイズやデザインをしております。 また、iPhoneアプリの開発もしております。

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
PAGE TOP ↑

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