<> Strange Error

2019-01-23 22:47发布

问题:

I have encountered a situation where xcode stops auto-completing and if you try to write a variable that already been defined xcode says << error type>>.

Here is my error:

回答1:

Often this indicates that your code doesn't currently compile. Swift often has trouble computing types on code that itself isn't correct. In some cases it's a bug in the compiler. Use of AnyObject can be particularly confusing to the compiler, and should be avoided as much as possible. In this case, AnyObject is required, but you should try to get it converted to a specific type quickly. Don't return [AnyType] for instance if you can possibly help it.

But the short answer is that the Swift compiler is still evolving, and it can't always work out types in complex situations, particularly on partial or (currently) incorrect code.

Note that you're using var for a lot of things that should be let. Unless you actually need to modify the variable, you should prefer let. It helps you prevent many kinds of bugs, and can be easier on the compiler to deal with (since the variable has fewer ways it can change).



回答2:

I have started to see same errors after Xcode 6.1 and iOS 8.1 update. I have found that if you delete ModuleCache file at /Users/username/Library/Developer/Xcode/DerivedData/ModuleCache path, it fixes the error temporarily. And you don't have to close Xcode or the project while doing that. After deleting the file, just wait for Xcode a little to index the project files again. After that, the problem mostly resolves for some time.

As it doesn't solve the problem permanently, you should avoid writing codes causing this according to other answers till Apple solves this.



回答3:

<<error type>> can result from the Swift compiler not finding the header file.

Same module:

Do you have some sort of folder structure that your source code is in? If so, try setting Scan All Source Files for Includes to YES.
This will make Xcode look through all of those folders when trying to find the Header file.

Embedded Projects, multiple modules etc:

1. Check the Search Paths

Have a look where the file in which your type is defined is stored. In your Build Settings make sure that this location is included in the Search Paths.
If it is part of the User Header Search Paths, make sure, that Always Search User Paths is turned on.

2. Check dependencies

Have a look at superclasses etc of your type. Are they included in the Search Path as well?



回答4:

Found out that the error goes away and autocomplete works again if i initialised the variable like this:

var name: String = "my name"

instead of :

var name = "my name" as String


回答5:

Firstly, try CMD-SHIFT-K followed CMD-R to clean and then run the build.

If that doesn't work, close XCode, delete derived data, and reopen XCode.



标签: swift xcode6