I am creating an application to teach myself swift and iOS in general. In this application, I wish to create an array of "Restaurant" objects, and allow users to create a new Restaurant and append it to the array. Although, when I create a new Restaurant instance, I believe it is just copying the array, and therefore not actually appending anything to it. I was wondering if i could create one array in swift that is accessible to all files, without having to create an instance of that array in another file. (Code Sample below)
/* The following code is in one view controller*/
// Create an array of restuarant instances to use within the application.
var restaurants:[Restaurant] = [
Restaurant(name: "Cafe Deadend", type: "Coffee & Tea Shop",
location: "G/F, 72 Po Hing Fong, Sheung Wan, Hong Kong",
phoneNumber: "232-923423", image: "cafedeadend.jpg",
isVisited: false),
Restaurant(name: "Homei", type: "Cafe", location: "Shop B, G/F, 22-
24A Tai Ping San Street SOHO, Sheung Wan, Hong Kong",
phoneNumber: "348-233423", image: "homei.jpg", isVisited:
false),
Restaurant(name: "Teakha", type: "Tea House", location: "Shop B, 18
Tai Ping Shan Road SOHO, Sheung Wan, Hong Kong",
phoneNumber: "354-243523", image: "teakha.jpg", isVisited:
false)
]
// Get properties to create a new restaurant object
@IBOutlet weak var restaurantName: UITextField!
@IBOutlet weak var restaurantType: UITextField!
@IBOutlet weak var restaurantLocation: UITextField!
@IBOutlet weak var restaurantPhoneNumber: UITextField!
@IBOutlet weak var restaurantImageName: UITextField!
@IBOutlet weak var restaurantIsVisited: UITextField!
// Function for when Add Restaurant button is clicked.
@IBAction func addRestaurant(sender: UIButton) {
var isVisited = false
// Create local vaiables to the properties once a user enters a new restaurant.
let name = self.restaurantName.text!
let type = self.restaurantType.text!
let location = self.restaurantLocation.text!
let phoneNumber = self.restaurantPhoneNumber.text!
let image = self.restaurantImageName.text!
// check to see what the text inside the text field for is visited is, and then set the variable accordingly.
if self.restaurantIsVisited.text! == "Yes" {
isVisited = true
} else {
isVisited = false
}
//here we add the text into a new restaurant object by invoking the function below:
createNewRestaurantObjectAndAddItToArrayOfObjects(name, type: type,
location: location, number: phoneNumber, image: image,
isVisited: isVisited)
}
/* Function name speaks for itslef :)
this method may be unnessecary, but i will go back and fix it
later
*/
func createNewRestaurantObjectAndAddItToArrayOfObjects(name:
String, type: String, location: String, number: String, image:
String, isVisited: Bool) {
let newRestaurant = Restaurant(name: name, type: type,
location: location, phoneNumber: number, image: image,
isVisited: isVisited)
// here i append the new restaurant on to the array of rest objects.
restaurants.append(newRestaurant)
print(restaurants.count)
print(restaurants[21])
}
// The code from here on is how i access the array from another swift file,
var restaurants = AddRestaurantViewController().restaurants
I hope this code sample helps explain my problem. Thanks!