15

I am defining a struct like this in the Point.swift inside of my Xcode Project file:

struct Point {
    var x: Int
    var y: Int
}

If i am trying to init the Point struct from another file, Xcode does not autocompleting the memberwise initializer. Even if I have been restarted the machine as some friends suggest.

screenshot

BUT autocompletion works fine, if i am defining initialising a new Instance in the same file or playground.

screenshot

Any ideas how to fix this autocompletion issue?

muizidn
  • 64
  • 2
  • 9
Alex
  • 578
  • 6
  • 19

7 Answers7

13

This appears to just be a bug in Xcode. The first time you use an instance in another file, Xcode does not provide the autocompletion. But, the second time you use it it does.

Here is a demo of the strange behavior I am seeing. Even with the Point.swift file saved, the first time I use Point in ViewController.swift it doesn't autocomplete, but the second time it does:

Demo of strange autocomplete behavior

This is with Xcode 7.2.

vacawama
  • 150,663
  • 30
  • 266
  • 294
  • I'm seeing the exact same behavior in Xcode 7.3beta. – vacawama Jan 30 '16 at 13:10
  • I tried using `Point` in another method after using it in `viewDidLoad` and it didn't autocomplete. It seems to only work if you've used it once already in the same method. – vacawama Jan 30 '16 at 13:27
  • I'm seeing similar behaviour in Xcode 9.3, but only outside of a function. (It works for me inside viewDidLoad and other functions.) I found that you don't even have to complete the constructor with arguments the first time—just typing 'let p = Point' on one line is enough to get autocomplete working on the next. How odd. – Kal May 22 '18 at 03:57
  • 1
    I can confirm that this is still a bug in Xcode10, and the suggested solution (autocomplete will show when typing for the second time) still works for me. – Andrej Feb 12 '19 at 13:19
  • Still a bug in xcode 11. Also depending on sometimes the .init(...) and other workaround work, sometimes not. – jalone Mar 12 '20 at 17:45
8

This bug is still actual in Xcode 10. As an addition to top answer: you can use explicit .init for autocomplete and then delete .init.

  1. Get autocomplete to work with explicit init:

Point.init(x: x, y: y)

  1. Delete .init and you'll end up with the expected code:

Point(x: x, y: y)

Andrej
  • 7,266
  • 4
  • 38
  • 57
mbabaev
  • 353
  • 2
  • 6
  • From Review: Hi, this post does not seem to provide a [quality answer](https://stackoverflow.com/help/how-to-answer) to the question. Please either edit your answer and improve it, or just post it as a comment to the other answer. – sɐunıɔןɐqɐp Nov 27 '18 at 14:32
5

As Михаил Бабаев also said, I've still seen this issue in Xcode 10.1.

Typing Point.init( brings up the default initialisers. Then you could delete the 'init'

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
eby
  • 111
  • 1
  • 4
3

Save your files.

Xcode will autocomplete correctly once you've saved Point.swift

I know it seems weird, but although Xcode recognizes the struct and enables syntax coloring properly while editing the files, it still doesn't autocomplete completely until the struct source file is definitely saved (the file icon will change its background color).

I've had this behaviour in the past, and I was just able to reproduce it in a sample project.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • 2
    I thought the same thing. But that isn't it. Even with all files saved, the first time you use `Point` in another file, it doesn't autocomplete. The second time you use `Point` it does autocomplete. – vacawama Jan 30 '16 at 12:41
  • @vacawama Mm. In a new project I can reproduce it like in my answer, and I can't reproduce it like in your comment. This is so weird. :P – Eric Aya Jan 30 '16 at 12:44
  • I'm using Xcode Version 7.2 (7C68). I defined `Point` as a `struct` in a file called `Point.swift`. Have you done anything differently? – vacawama Jan 30 '16 at 12:47
  • 7.3beta, but exactly the same process as you and OP. I also made tests with deleting/saving/pasting without saving, etc. Same: it behaves exactly like OP then I save Point.swift and it behaves like in my answer. – Eric Aya Jan 30 '16 at 12:52
  • @EricD. saved the Point.swift file, but still not getting the autocompletion in another file – Alex Jan 30 '16 at 12:57
  • I added a demo of the behavior I'm seeing in Xcode 7.2. – vacawama Jan 30 '16 at 13:05
  • @vacawama Yeah, it confirms OP's issue that it doesn't work the first time you type it in another file than the one it's declared in. My experience was: create Point.swift, paste/type struct definition, don't save, go in viewcontroller then type an instance - you got syntax coloring and type recognition but not autocompletion, then save Point.swift: now you also got completion. – Eric Aya Jan 30 '16 at 13:10
1

If you type Point.init( Xcode suggests the init autocompletion.

More exciting news: starting from Xcode 10.2 this seems to be fixed, now if you tipe just Point( Xcode suggests the init method. Enjoy it.

Massimo Polimeni
  • 4,826
  • 4
  • 27
  • 54
0

I hit this same issue with Xcode Version 11.4.1 (11E503a). For me the issue was that my struct was defined in a different target than my app target. That target was imported in the file (in app target) I was trying to create an instance of that struct in. However, Xcode was never able to autocomplete the struct. I solved this by moving the definition of the struct to the app target and it autocompleted the memberwise initializer as expected.

Are we having fun yet?

Pouria Almassi
  • 1,580
  • 2
  • 17
  • 26
0

For me it was a self induced error. I couldn't figure out why it wouldn't offer the autocompletion option. It took a few inspections of the struct to realize that even though I had a bunch of properties they were all defined as static - there was nothing to initialize! smh

Mozahler
  • 4,958
  • 6
  • 36
  • 56