ios - 可从所有文件访问 Swift 数组

标签 ios arrays swift

我正在创建一个应用程序来自学 swift 和 iOS。在此应用程序中,我希望创建一个“Restaurant”对象数组,并允许用户创建一个新餐厅并将其附加到该数组中。虽然,当我创建一个新的 Restaurant 实例时,我相信它只是复制数组,因此实际上并没有向其中添加任何内容。我想知道是否可以在 swift 中创建一个可供所有文件访问的数组,而不必在另一个文件中创建该数组的实例。 (下面的代码示例)

    /* 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

我希望此代码示例有助于解释我的问题。谢谢!

最佳答案

创建类变量,您可以使用类名访问它。

请找到以下代码

static 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)
]

要使用此变量,您必须使用类而不是实例,如下所示

AddRestaurantViewController.restaurants.append(newRestaurant)
    print(AddRestaurantViewController.restaurants.count)
    print(AddRestaurantViewController.restaurants[21])

您可以从类外部使用此变量,如下所示

var restaurants = AddRestaurantViewController().restaurants

关于ios - 可从所有文件访问 Swift 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36732093/

相关文章:

通过 uiviewcontroller 显示的自定义 View 的 iOS 旁白

c++ - 无法填充动态类模板数组

c - 如何在 C 中正确分配字符串数组中的指针?

ios - 使用未解析的标识符 FBSession

ios - 色调正在快速反转颜色

ios - 在文本字段之间更改焦点时保持键盘打开

ios - UIViewControllerAnimatedTransitioning 每隔一段时间才有效?

swift - 物理多次检测碰撞

iphone - 无法打开文档 ".xib"。无法读取存档

arrays - 如何更新字典数组中的字典值