Sunday, November 8, 2026
HomeiOS Developmentios - Can not use occasion member 'generateRandomNumbers' inside property initializer; property...

ios – Can not use occasion member ‘generateRandomNumbers’ inside property initializer; property initializers run earlier than ‘self’ is on the market

[ad_1]

You might be asking var array to obtain a “default” worth from the operate generateRandomNumbers(). The “default” worth is about through the initialisation of the occasion of your view controller, so earlier than the occasion exists, so earlier than the generateRandomNumbers() operate exists. The compiler would not know learn how to give a price to array earlier than the occasion has completed being created.

A workaround is to set a dummy default worth, then change it throughout initialisation, like this:

@IBOutlet weak var tableView1: UITableView!
var array = [Int]()    // That is an empty array, the compiler can create it earlier than the generateRandomNumbers() is on the market
override func viewDidLoad() {
    tremendous.viewDidLoad()
    // Do any extra setup after loading the view.
    tableView1.delegate = self
    tableView1.dataSource = self
    ...

    array = generateRandomNumbers(measurement: 1000)     // Now that every one variables are initialised, you'll be able to change their values

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments