Skip to content

Commit 9647f2d

Browse files
committed
Update JSON example to use new API
1 parent fc7bbbd commit 9647f2d

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

config/ConfigExample/AppDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414

1515
import UIKit
16-
import Firebase
16+
import FirebaseCore
1717

1818
@UIApplicationMain
1919
class AppDelegate: UIResponder, UIApplicationDelegate {

config/ConfigExample/RemoteConfigViewController.swift

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@
1313
// limitations under the License.
1414

1515
import UIKit
16-
import Firebase
16+
import FirebaseRemoteConfig
17+
import FirebaseRemoteConfigSwift
1718

1819
class RemoteConfigViewController: UIViewController {
1920
private var remoteConfig: RemoteConfig!
2021
private var remoteConfigView: RemoteConfigView { view as! RemoteConfigView }
2122

2223
private let topLabelKey = "topLabelKey"
2324
private let recipeKey = "recipeKey"
25+
// The JSON value for typedRecipeKey match recipeKey except ints are Ints instead of String.
26+
private let typedRecipeKey = "typedRecipeKey"
2427
private let bottomLabelKey = "bottomLabelKey"
2528

2629
override func loadView() {
@@ -136,20 +139,35 @@ class RemoteConfigViewController: UIViewController {
136139
"serving_size",
137140
"notes",
138141
]
139-
guard let recipeDictionary = remoteConfig[recipeKey].jsonValue as? [String: Any] else { return }
140-
141-
keys.enumerated().forEach { index, key in
142-
guard var value = recipeDictionary[key] else { return }
142+
struct Recipe: Decodable {
143+
var recipe_name: String
144+
var ingredients: [String]
145+
var prep_time: Int
146+
var cook_time: Int
147+
var instructions: [String]
148+
var yield: String
149+
var serving_size: Int
150+
var notes: String
151+
}
143152

153+
guard let recipe: Recipe = try? remoteConfig[typedRecipeKey].decoded() else {
154+
fatalError("Failed to decode JSON for \(typedRecipeKey)")
155+
}
156+
let mirror = Mirror(reflecting: recipe)
157+
var index = 0
158+
for (property, value) in mirror.children {
159+
guard let key = property else { continue }
160+
var stringValue: String
144161
if let list = value as? [String] {
145-
let joinedValue = list.joined(separator: "")
146-
value = joinedValue
147-
} else if let stringValue = value as? String {
148-
value = stringValue
162+
stringValue = list.joined(separator: "")
163+
} else if let intVal = value as? Int {
164+
stringValue = String(intVal)
165+
} else if let val = value as? String {
166+
stringValue = val
167+
} else {
168+
fatalError("Unrecognized type for \(key)")
149169
}
150170

151-
guard let stringValue = value as? String else { return }
152-
153171
let formattedKey = key
154172
.capitalized
155173
.replacingOccurrences(of: "_", with: " ")
@@ -182,6 +200,7 @@ class RemoteConfigViewController: UIViewController {
182200
let y: CGFloat = step * CGFloat(index) + offset
183201

184202
label.frame.origin = CGPoint(x: x, y: y)
203+
index += 1
185204
}
186205
}
187206

config/Podfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ target 'ConfigExample' do
66
use_frameworks!
77

88
# Pods for ConfigExample
9-
pod 'Firebase/RemoteConfig'
9+
pod 'FirebaseRemoteConfig', :path => "/Users/paulbeusterien/gh5/firebase-ios-sdk"
10+
pod 'FirebaseRemoteConfigSwift', :path => "/Users/paulbeusterien/gh5/firebase-ios-sdk"
11+
pod 'FirebaseSharedSwift', :path => "/Users/paulbeusterien/gh5/firebase-ios-sdk"
1012

1113
target 'ConfigExampleTests' do
1214
inherit! :search_paths

0 commit comments

Comments
 (0)