简体   繁体   English

如何在Xcode Swift中从其他视图引用数组?

[英]How to refer to an array from other view in Xcode Swift?

I have a question regarding how to refer to an array from other view (other cocoa touch file) in Swift. 我有一个关于如何在Swift中从其他视图(其他可可触摸文件)引用数组的问题。

I created a project where there are two views: BeerInfoView and BeerRatingView. 我创建了一个项目,其中有两个视图:BeerInfoView和BeerRatingView。 BeerRatingView has a slider button (for rating beer from 1 to 5) and a text view field (for text review), and the values from both slider and textview field are currently saved locally by NSUserDefaults [ eg NSUserDefaults.standardUserDefaults().setObject(writeReview, forKey:"writeReview"); BeerRatingView有一个滑块按钮(用于将啤酒定为1到5)和一个文本视图字段(用于文本查看),并且滑块和textview字段的值当前都由NSUserDefaults [例如,NSUserDefaults.standardUserDefaults()。setObject( writeReview,forKey:“ writeReview”); ]. ]。

BeerInfoView contains a table which will list all the reviews posted from the BeerRatingView (prototype cell has three labels that will contain rating, text review, etc). BeerInfoView包含一个表,该表将列出从BeerRatingView发布的所有评论(原型单元具有三个标签,其中将包含评分,文本评论等)。 Currently, BeerInfoView.swift contains a hardcoded array so the table only displays a limited list of pre-written reviews. 当前,BeerInfoView.swift包含一个硬编码数组,因此该表仅显示有限的预写评论列表。

What I want to do is to make the app automatically append and put the locally saved data into an aforementioned hardcoded array every time a user clicks on the "Post" button from the BeerRatingView, but I'm having a hard time because I'm not sure how to call / refer to an array from other view (BeerInfoView) in the BeerRatingView. 我想做的是使该应用程序每次用户在BeerRatingView中单击“发布”按钮时都自动追加并将本地保存的数据放入前面提到的硬编码数组中,但是我很难过,因为不知道如何在BeerRatingView中从其他视图(BeerInfoView)调用/引用数组。

Here are portions of codes from BeerInfoView.swift and BeerRatingView.swift: 以下是BeerInfoView.swift和BeerRatingView.swift的部分代码:

------- BeerInfoView.swift --------- ------- BeerInfoView.swift ---------

var arrayOfBeerUsers: [BeerUser] = [BeerUser]()  

func setUpBeerUsers() {
    var user1 = BeerUser(username: "dave", review: "pretty meh", rating: 2)
    var user2 = BeerUser(username: "liz", review: "pretty good", rating: 4)
    var user3 = BeerUser(username: "michael", review: "I can make it better", rating: 1)

    arrayOfBeerUsers.append(user1)
    arrayOfBeerUsers.append(user2)
    arrayOfBeerUsers.append(user3)
}

------- BeerRatingView.swift ----------- ------- BeerRatingView.swift -----------

@IBAction func postButtonTapped(sender: AnyObject) {

    let writeReview = writeReviewTextView.text

    if (writeReview == "Write a Review:") {
        displayMyAlertMessage("review field cannot be empty!")
        return
    }
    // check if user didn't move around the slider
    else if (displayRatingLabel.text == "Move slider to rate from 1 to 5!") {
        displayMyAlertMessage("You didn't leave the rating!")
        return
    }
    else {

        // saving "text review" locally
        NSUserDefaults.standardUserDefaults().setObject(writeReview, forKey:"writeReview");

        NSUserDefaults.standardUserDefaults().synchronize();

        let textReviewStored = NSUserDefaults.standardUserDefaults().stringForKey("writeReview");



        var beerInfoViewController: BeerInfoViewController = self.storyboard?.instantiateViewControllerWithIdentifier("BeerInfoViewController") as BeerInfoViewController

        let beerArray = beerInfoViewController.arrayOfBeerUsers    //// is this how you refer to an array from BeerInfoView??

        beerArray.append(username: usernameStored, review: textReviewStored, rating: ratingStored)


        self.dismissViewControllerAnimated(true, completion:nil);

    }

} 

Swift arrays are value types, and as such they are passed not by reference but by value, which means a copy is created when an array is passed to a function, assigned to a variable, etc. Swift数组是值类型,因此它们不是通过引用传递而是通过值传递,这意味着在将数组传递给函数,分配给变量等时创建副本。

In order to pass an array (and more generally any value type) to a function by reference, you can use the inout modifier in the function declaration, and use the reference operator & when passing the argument to the function. 为了通过引用将数组(通常是任何值类型)传递给函数,可以在函数声明中使用inout修饰符,并在将参数传递给函数时使用引用运算符&。 In your case: 在您的情况下:

func ExcLowerDidFinish(controller: String, inout Array:[Int])

and: 和:

delegate!.ExcLowerDidFinish(self, Array: &ExcUpperArray)

Off topic: note that by convention in swift functions/methods and variables/parameters names start with lowercase, whereas types (classes, structs, etc.) start with uppercase. 主题外:请注意,按惯例,快速函数/方法和变量/参数名称以小写开头,而类型(类,结构等)以大写开头。 Your code may be hard to read by other Swift developers 您的代码可能很难被其他Swift开发人员阅读

And refer to the url.. 并参考网址。

http://www.raywenderlich.com/75289/swift-tutorial-part-3-tuples-protocols-delegates-table-views http://www.raywenderlich.com/75289/swift-tutorial-part-3-tuples-protocols-delegates-table-views

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM