Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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)
Posted
Updated 11-Jan-22 7:03am
v3
Comments
Richard MacCutchan 11-Jan-22 14:38pm    
Your previous question are based on Java, Javascript, Kotlin, WPF and C#. You really need to focus on a single language and study it in detail.

1 solution

I am not a Swift programmer. However, after reading https://docs.swift.org/swift-book/LanguageGuide/ClassesAndStructures.html[^] I would make the following suggestion. The task is to create an array of City objects, not strings of city names. So you need something like:
Swift
let miami = City(id:1, name:"Miami", description:"A place in Florida", latitude:25.7823404, longitude:-80.3695441)

You have declared latitude and longitude as doubles, so you will need to use Google maps, or similar, to find the actual values.
 
Share this answer
 
v2
Comments
Member 15127345 11-Jan-22 13:04pm    
Appreciate your help! I've updated my question regarding this problem
Richard MacCutchan 11-Jan-22 14:36pm    
I am afraid I cannot answer your updated questions. As I said, I am not a Swift programmer (in fact quite slow).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900