I have a question regarding how to refer to an array from other view (other cocoa touch file) in Swift.
I created a project where there are two views: BeerInfoView and 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"); ].
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). Currently, BeerInfoView.swift contains a hardcoded array so the table only displays a limited list of pre-written reviews.
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.
Here are portions of codes from BeerInfoView.swift and BeerRatingView.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 -----------
@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.
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. 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
And refer to the url..
http://www.raywenderlich.com/75289/swift-tutorial-part-3-tuples-protocols-delegates-table-views
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.