Skip to content

Commit 5020a04

Browse files
authored
DOC-8512 -- Apply swift code feedback (couchbase#471)
https://issues.couchbase.com/browse/DOC-8512
1 parent fd67253 commit 5020a04

File tree

1 file changed

+43
-43
lines changed

1 file changed

+43
-43
lines changed

modules/swift/examples/code_snippets/SampleCodeTest.swift

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,6 +2103,9 @@ class QueryResultSets {
21032103

21042104
// QUERY RESULT SET HANDLING EXAMPLES
21052105
// tag::query-syntax-all[]
2106+
let db = try! Database(name: "hotel")
2107+
var hotels = [String:Any]()
2108+
var hotel:Hotel = Hotel.init()
21062109

21072110
let listQuery = QueryBuilder.select(SelectResult.all())
21082111
.from(DataSource.database( db))
@@ -2113,45 +2116,38 @@ class QueryResultSets {
21132116

21142117
do {
21152118

2116-
for (row) in try! listQuery.execute() {
2119+
for row in try! listQuery.execute() {
21172120

2118-
print(row.toDictionary())
2121+
let thisDocsProps =
2122+
row.dictionary(at: 0)?.toDictionary() // <.>
21192123

2120-
if let hotel = row.dictionary(forKey: "_doc") { // <.>
2124+
let docid = thisDocsProps!["id"] as! String
21212125

2122-
let hotelid = hotel["id"].string!
2126+
let name = thisDocsProps!["name"] as! String
21232127

2124-
hotels[hotelid] = hotel.toDictionary()
2128+
let type = thisDocsProps!["type"] as! String
21252129

2130+
let city = thisDocsProps!["city"] as! String
21262131

2127-
if let thisDocsProperties =
2128-
hotel.toDictionary() as? [String:Any] { // <.>
2132+
let hotel = row.dictionary(at: 0)?.toDictionary() //<.>
21292133

2130-
let docid = thisDocsProperties["id"] as! String
2134+
let hotelId = hotel!["id"] as! String
21312135

2132-
let name = thisDocsProperties["name"] as! String
2133-
2134-
let type = thisDocsProperties["type"] as! String
2135-
2136-
let city = thisDocsProperties["city"] as! String
2137-
2138-
print("thisDocsProperties are: ", docid,name,type,city)
2139-
2140-
} // end if
2141-
2142-
} // end if
2136+
hotels[hotelId] = hotel
21432137

21442138
} // end for
2145-
} catch let err {
2146-
print(err.localizedDescription)
2147-
// ... handle errors as required
2139+
21482140
} //end do-block
21492141

21502142
// end::query-access-all[]
21512143

21522144
//
21532145
func dontTestQueryProps () throws {
21542146
// tag::query-syntax-props[]
2147+
let db = try! Database(name: "hotel")
2148+
var hotels = [String:Any]()
2149+
var hotel:Hotel = Hotel.init()
2150+
21552151
let listQuery = QueryBuilder
21562152
.select(SelectResult.expression(Meta.id).as("metaId"),
21572153
SelectResult.expression(Expression.property("id")),
@@ -2165,32 +2161,33 @@ class QueryResultSets {
21652161
// tag::query-access-props[]
21662162
for (_, result) in try! listQuery.execute().enumerated() {
21672163

2168-
print(result.toDictionary())
2169-
2170-
if let thisDoc = result.toDictionary() as? [String:Any] {
2171-
2172-
let docid = thisDoc["metaId"] as! String
2173-
2174-
let hotelId = thisDoc["id"] as! String
21752164

2176-
let name = thisDoc["name"] as! String
2165+
let thisDoc = result.toDictionary() as? [String:Any] // <.>
2166+
// Store dictionary data in hotel object and save in array
2167+
hotel.id = thisDoc!["id"] as! String
2168+
hotel.name = thisDoc!["name"] as! String
2169+
hotel.city = thisDoc!["city"] as! String
2170+
hotel.type = thisDoc!["type"] as! String
2171+
hotels[hotel.id] = hotel
21772172

2178-
let city = thisDoc["city"] as! String
2179-
2180-
let type = thisDoc["type"] as! String
2181-
2182-
// ... process document properties as required
2183-
print("Result properties are: ", docid, hotelId,name, city, type)
2184-
}
2185-
}
2173+
// Use result content directly
2174+
let docid = result.string(forKey: "metaId")
2175+
let hotelId = result.string(forKey: "id")
2176+
let name = result.string(forKey: "name")
2177+
let city = result.string(forKey: "city")
2178+
let type = result.string(forKey: "type")
21862179

2180+
// ... process document properties as required
2181+
print("Result properties are: ", docid, hotelId,name, city, type)
2182+
} // end for
21872183
// end::query-access-props[]
21882184
}
21892185

21902186
//
21912187
func dontTestQueryCount () throws {
21922188

21932189
// tag::query-syntax-count-only[]
2190+
let db = try! Database(name: "hotel")
21942191
do {
21952192
let listQuery = QueryBuilder
21962193
.select(SelectResult.expression(Function.count(Expression.all())).as("mycount"))
@@ -2200,13 +2197,15 @@ class QueryResultSets {
22002197

22012198

22022199
// tag::query-access-count-only[]
2200+
22032201
for result in try! listQuery.execute() {
2204-
if let dict = result.toDictionary() as? [String: Int] {
2205-
let thiscount = dict["mycount"]! // <.>
2206-
print("There are ", thiscount, " rows")
2207-
}
2208-
}
2202+
let dict = result.toDictionary() as? [String: Int]
2203+
let thiscount = dict!["mycount"]! // <.>
2204+
print("There are ", thiscount, " rows")
2205+
} // end for
2206+
22092207
} // end do
2208+
22102209
} // end function
22112210

22122211
// end::query-access-count-only[]
@@ -2215,6 +2214,7 @@ class QueryResultSets {
22152214
func dontTestQueryId () throws {
22162215

22172216
// tag::query-syntax-id[]
2217+
let db = try! Database(name: "hotel")
22182218
let listQuery = QueryBuilder.select(SelectResult.expression(Meta.id).as("metaId"))
22192219
.from(DataSource.database(db))
22202220

0 commit comments

Comments
 (0)