0

I have a SearchURL String which gets a chose variable from previous view controller. And with this gotten variable, SearchURL should be used in callAlamo func. But I have an error:

enter image description here

Should I use async dispatch or something like this? I've tried many things like putting everything in viewDidLoad() but did not work. Anybody could help?

belab
  • 117
  • 3
  • 12
  • See also http://stackoverflow.com/questions/25854300/how-to-initialize-properties-that-depend-on-each-other and http://stackoverflow.com/questions/38651085/instance-member-cannot-be-used-on-type-of-custom-class. – Martin R May 03 '17 at 19:29
  • 1
    @MartinR I have seen. Did not help. – belab May 03 '17 at 19:52
  • @MartinR For the record man, not a duplicate of those 3 questions. His question was more about passing values across VCs (and then initializing properties, which those questions address) *plus* a tricky string bug in it. BTW, his issue was completely solved :) – Paulo Mattos May 03 '17 at 20:21
  • @PauloMattos: For the record: Everything I can see in the *question* is a *compiler error* due to an instance property whose initial value depends on another property. The various possible solutions (lazy variable, computed property, move initialisation to a method) are all contained in the answers of above threads. – I *may* be that the real problem was something else, but then that information is not contained in the question (and the question therefore unclear). – Martin R May 03 '17 at 20:29
  • 1
    @MartinR Anyway bro, he is a rookie coder -- we all been there :) He can't (yet!) fix his issue by looking at related questions -- he needs *hands on* help with his code (and English isn't his first language, making matters worse). No need to scary him away from SO... let's help him up the ladder together :) – Paulo Mattos May 03 '17 at 20:43

2 Answers2

2

You can use a computed property like so:

var searchURL: String {
   return "https://theurlIcantcopybecauseitsascreenshot.com/\(chosed!)"
}

Note that this will be recomputed every time you access it, so if chosed changes, so will searchURL. You can also make it an implicitly unwrapped optional like in Paulo Mattos' answer, or you could make an initializer that takes chosed as a parameter (see comments below, for a caveat). Then you could set searchURL in init.

Also, just a nitpick to let you know, Swift standard is to have variables in camel case (searchURL instead of SearchURL).

mfaani
  • 33,269
  • 19
  • 164
  • 293
Connor Neville
  • 7,291
  • 4
  • 28
  • 44
1

The viewDidLoad will not be called everytime you enter your view controller, as such, you may not detect updates to the chosed property (among other tricky issues).

You could do it in viewWillAppear method instead:

var SearchURL: String!

overide func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    SearchURL = "...\(chosed!)..."
    ...
}

Or you could use a lazy variable:

lazy var SearchURL = {
    return "...\(self.chosed!)..."
}()

Or use a computed property as suggested by Connor Neville below.

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85