Hello,
I have a test variable here which works fine:
var quotes: [(quote: String, order: Int)] = [
("I live you the more ...", 1),
("There is nothing permanent ...", 2),
("You cannot shake hands ...", 3),
("Lord, make me an instrument...", 4)
]
and I have a test function which successfully pulls data from a mysql database via a web service and displays it via the "print" function:
func getPrice(){
if let url = URL(string:"https://www.TEST.com/test_connection.php"){
URLSession.shared.dataTask(with: url) { (data, response, error) in
if let data = data{
if let json = try? JSONDecoder().decode([[String:String]].self, from: data){
json.forEach { row in
print(row["quote"]!)
print(row["order"]!)
}
}
else{
}
}
else{
print("wrong :-(")
}
}.resume()
}
}
Please can you tell me how to re-write the quotes variable/array so that it returns the results that are found in the getPrice() function?
Post
Replies
Boosts
Views
Activity
Hello,
This test code for creating an array using a loop works:
var quotes: [(id: String, name: String)] {
var output: [(id: String, name: String)] = []
for i in 1...numberOfRows {
let item: (id: String, name: String) = ("\(i)", "Name \(i)")
output.append(item)
}
return output
}
But if I try to apply this logic to retrieving data from a web service using the below code I am getting 2 errors:
For the line “quotes.append(item)” I am getting the error message “Cannot use mutating member on immutable value: ‘quotes’ is a get-only property."
For the line “return output” I am getting the error message “Cannot find ‘output’ in scope."
if let url = URL(string:"https://www.TEST.com/test_connection.php"){
URLSession.shared.dataTask(with: url) { (data, response, error) in
if let data = data{
if let json = try? JSONDecoder().decode([[String:String]].self, from: data){
json.forEach { row in
var item: (id: String, name: String) = ("test id value", "test name value")
quotes.append(item)
}
return output
}
}
}
}
Hello,
Please can you tell me how to create an array of dictionaries? This code below should create 4 dictionaries in an array, but I'm getting these errors:
For line "var output = [id: "testID", name: "testName"]":
cannot find 'name' in scope
Type '(any AnyObject).Type'
cannot conform to 'Hashable'
For line "return output":
Type '(any AnyObject).Type' cannot conform to 'Hashable'
var quotes: [(id: String, name: String)] {
var output = [[(id: String, name: String)]] ()
for i in 1...4 {
var output = [id: "testID", name: "testName"]
}
return output
}
Hi the below array and code to output a list item works fine:
var quotes = [
[
"quote": "I live you the more ...",
"order": "1"
],
[
"quote": "There is nothing permanent ...",
"order": "2"
],
[
"quote": "You cannot shake hands ...",
"order": "3"
],
[
"quote": "Lord, make me an instrument...",
"order": "4"
]
]
cell.textLabel?.text = quotes[indexPath.row]["quote"]
However if I change the "order" values to be numbers rather than text like below then for the above line I get an error message in Xcode "No exact matches in call to subscript". Please could someone tell me how to make it work with the numbers stored as numbers? (I'm wondering if creating an any array type and using the .text function has caused a conflict but I can't find how to resolve)
[
"quote": "I live you the more ...",
"order": 1
],
[
"quote": "There is nothing permanent ...",
"order": 2
],
[
"quote": "You cannot shake hands ...",
"order": 3
],
[
"quote": "Lord, make me an instrument...",
"order": 4
]
]
Thank you for any pointers :-)
Hello,
I have installed the latest Xcode (v 16.2). I would like to code for minimum iOS installation v 16. The default Hello World code in Xcode 16.2 has error messages because some of the code requires minimum target installation of iOS v 17.
Please can you tell me how to get the default Hello World code for minimum target iOS 16 to show in Xcode 16?
(I considered installing Xcode 14, but the minimum Xcode for deployment is v15)
Thank you for any help