简体   繁体   中英

Swift How to choose a random word from the array and put it in the list

I guess the question is pretty clear, I've been taking some courses and there's a challange that says

"Build a UI with a List and a Button below it.

Declare a property that contains an array of 5 strings. These can be any 5 words you want.

Each time the button is tapped, choose a random word from the array and put it in the list.

The number of items in the list should grow as you tap the button. "

and I couldn't figure out how to do it on my own and it eats me so I wanted to ask you guys even tho I know this may be too easy for you but for me its not, so I hope someone can help me so I can learn from your codes

Break the problem into steps. Tackle the steps one at a time.

Decide if you want to use UIKit or SwiftUI.

Figure out how to create a new iOS app project.

Figure out how to create a new, custom view controller, and how to add custom UI elements to that view controller. (At this point the UIKit and SwiftUI options will be very different)

Figure out how to add custom properties to your view controller's class, and how to add a property that contains an array of 5 strings.

Figure out how to add a button to your view controller that invokes an action.

Figure out how to add a list to your view controller (probably a UILabel in UIKit, or a Label in SwiftUI.)

Figure out how to select a random item from an array. (Hint: The Array class has a method who's name begins with "Random" that might be helpful.)

Write the action method that updates your list view by adding a new, random element to it, and attach that action to your button.

No, I'm not going to give you code. Go start working on that, and update your question with specific problems you run into in your attempt.

(If you're taking a course on Swift development, it seems unlikely that this is the first exercise. Usually such courses teach you small steps, each lesson building on the last lesson.)

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.

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