I'm just starting out my journey in Swift and I have a task to do that I need help with. First, I have to create a data model and an array:
Quote:
City model object should contain: id, name, description, latitude, longitude, list of keyword/tag (such as seaside, party, sport, music, nature etc)
Create array with 5 cities.
After that I have to create a function searching for a specific name or keyword and returning list of cities with searched name/keyword:
Quote:
create func with name as parameter which return list of cities with searched name
create func with keyword as parameter which return list of cities which contain specific keyword (in keyword list)
Just like I've explained below, my main problem is with the first part of this task
What I have tried:
I've created a City class and an array:
class City{
var id:Int?
var name:String?
var description:String?
var latitude:Double?
var longitude:Double?
}
let cities = ["Miami", "New Orlean", "London", "Belfast", "Berlin"]
I don't have a problem with creating functions, which is a second part of this task. My main problem lies in the first part. If I create an array of 5 cities, like so:
let cities = ["Miami", "New Orlean", "London", "Belfast", "Berlin"]
then how do I "create a function with keyword as parameter which return list of cities which contain specific keyword (in keyword list)" if there are no keywords in this array? In fact, there are no keywords in general in Swift arrays
Am I missing something? I don't understand how this would work. I would really appreciate your help!
UPDATE:
Following @Richard MacCutchan's advice, I've created a city object like this:
let miami = City(id:1, name:"Miami", description:"A place in Florida", latitude:25.7823404, longitude:-80.3695441)
I've also added constructor to the City class:
class City{
var id:Int?
var name:String?
var description:String?
var latitude:Double?
var longitude:Double?
init(id:Int, name:String, description:String, latitude: Double, longitude:Double){
self.id = id
self.name = name
self.description = description
self.latitude = latitude
self.longitude = longitude
}
}
Then I've created an array:
let cities = [miami]
The output of "print(cities)" is following:
[main.City]
My first question is: if I'd like to print actual values of cities, is there a way to do that?
Another thing: in a City class I have to declare a "list of keyword/tag (such as seaside, party, sport, music, nature etc)". How should it look like? Should I create a tuple, like this?
let list_of_keywords = (seaside, party, sport, music, nature)