Auto-populate Variables in Functions

Inside my function my variables that I created before my functions are not auto-populating like they do outside of a function. It makes me go a little slower, and I don't want this to be a bad habit for the rest of my coding career (just starting). Is there a way to fix this?

1 Like

Hi, please could you explain what you mean by auto-populating? Do you mean that the function’s arguments should have default values?

If so, see here: Default parameter values

No, I mean predictive text. I have created variables and usually XCode is able to predict my recent variables as a prediction for what I want to to type so I don’t have to type out my entire variable name every single time. This normally works well, but not when I am inside of a function. Is it because my variable is scoped outside of the function? I was watching a tutorial and it seemed to be working for the person in the tutorial.

Yes, that doesn't sound right to me. I assume this is in Xcode?

if you use the code below and start typing after let test = either globalP or functionInter then it should autosuggest the appropriate property.

let globalProperty = "Hello"

public struct DeleteMe {
    public private(set) var text = "Hello, World!"

    public init() {
    }
	
	func aFunction() {
		let functionInternalProperty = "Hello as well."
		
		let test = 
	}
}

This doesn't work for me either; on a brand new Xcode project and after indexing is complete. Only when I type the first letter of either globalProperty or functionInternalProperty do I get the remaining autocomplete suggestion.

I think that's intentional - it has to have a 'seed' from which to autocomplete - otherwise there's effectively an infinite number of options.

1 Like

Agreed. Otherwise there is a very high chance it will suggest the wrong thing. I've always come to expect it to show suggestions only from the first character anyway.