@propertyWrapper
struct TwelveOrLess {
private var number: Int
init() { self.number = 0 }
var wrappedValue: Int {
get { return number }
set { number = min(newValue, 12) }
}
}
なぜなら let value = try decodeIfPresent(String.self, forKey: key) で、 栄養 を String 型としてデコードしようと試みますが、 栄養 は [String] 型だからです。
以下のようなエラーが出ると思います。
typeMismatch(Swift.String, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "栄養", intValue: nil)], debugDescription: "Expected to decode String but found an array instead.", underlyingError: nil))
ではどのようにするのがよいのか。
一つは、引数のKey によって処理を出し分ける方法が考えられます。
Key によってデコード処理を出し分ける
以下のようなコードに変えれば問題なくデコードできます。
extension KeyedDecodingContainer {
func decode(_: [String].Type, forKey key: Key) throws -> [String] {
if key.stringValue == "食材" {
let value = try decodeIfPresent(String.self, forKey: key)
return value?.components(separatedBy: ",") ?? []
} else if key.stringValue == "栄養" {
let value = try decodeIfPresent([String].self, forKey: key)
return value ?? []
} else {
return []
}
}
}
Error: Failed to get Firebase project YOUR_PROJECT_NAME. Please make sure the project exists and your account has permission to access it.
どうも前回ログインから時間が経っていると発生する場合があるらしいです。
以下のコマンドでログインしなおしたら正常に動作しました。
$ firebase logout
$ firebase login
firebase emulators:start で失敗する
以下のようなエラー文が出ました。
$ firebase emulators:start
i emulators: Starting emulators: functions, firestore, hosting
⚠ functions: The following emulators are not running, calls to these services from the Functions emulator will affect production: database, pubsub
✔ functions: Using node@14 from host.
i firestore: Firestore Emulator logging to firestore-debug.log
⚠ firestore: Fatal error occurred:
Firestore Emulator has exited with code: 1,
stopping all running emulators
i hub: Stopping emulator hub
i functions: Stopping Functions Emulator
i firestore: Stopping Firestore Emulator
⚠ firestore: Error stopping Firestore Emulator
Having trouble? Try firebase [command] --help
$ npm i -g firebase-tools
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
/usr/local/opt/nvm/versions/node/v12.18.3/bin/firebase -> /usr/local/opt/nvm/versions/node/v12.18.3/lib/node_modules/firebase-tools/lib/bin/firebase.js
+ firebase-tools@9.5.0
updated 1 package in 30.88s
バージョン確認をすると8.7.0 が入っている。
$ firebase --version
8.7.0
以下のように解決しました。
参照しているfirebase を確認。
$ which firebase
/usr/local/bin/firebase
npm のグローバルディレクトリの確認。
$ npm bin -g
/usr/local/opt/nvm/versions/node/v12.18.3/bin
(not in PATH env variable)
Analyzing dependencies
[!] `FirebaseAnalytics` requires CocoaPods version `>= 1.10.0`, which is not satisfied by your current version, `1.9.3`.
という訳で1.10.0 を指定してアップデートを試みる。
$ gem install cocoapods -v 1.10.0
Fetching cocoapods-1.10.0.gem
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.6.0 directory.
パーミッションがない🤔 sudo をつけて再度実行。
$ sudo gem install cocoapods -v 1.10.0
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /usr/bin directory.