Swift XCode getting internal errors when using enums - xcode

When using dot notation for enums using Swift, I always get internal errors (XCode kind of crashes)
For example
enum WeatherData {
case current, forecast, forecastTenDay, yesterday
}
let dataType: WeatherData = .current
As soon as I type in the period, it all goes weird.
No syntax coloring temporarily, and although it recovers, it's really annoying.
I use MacBook Pro (Retina, Mid 2012).
Anyone else getting this error?

Related

Xcode 8 does not auto-complete for init methods

I am having trouble getting Xcode 8 with Swift 3 to auto-complete common initialization methods, and was wondering if there is anything I could do to fix the situation.
Specifically, take something like:
let jsonString = String(data: jsonData, encoding: .utf8)
The initialization method String(data:, encoding:) will not populate.
Demo Video
I find it useful to type the actual word .init before asking for code completion. Then later, I take it back out again.
Note that Xcode is very choosy; if you go too far and type .init( it won't work.
EDIT According to Apple, this problem is fixed in Xcode 10.

Visual Studio Intellisense error in range-based for loop

My Visual Studio 2015 IDE (Community Edition) keeps complaining about the code below
struct item
{
int data;
std::vector<item*> linkedItems;
};
void traverseLinkedItems(item* p_item)
{
for (item* i : p_item->linkedItems) // Intellisense Error: A value of type "item*" cannot be used to initialize an entity of type "item*"
{
std::cout << i->data;
}
}
It compiles just fine. So I'm not sure whether it's a bug in VS, or am I missing something?
When I use auto instead of specifying the type of i explicitly, everything is OK.
Thanks!
IntelliSense is not always right. In order to know everything about your code, it would have to fully compile it, but this would be too slow to be (conveniently) usable. Instead, it parses your code in a faster but less complete way to get autocompletion information and find obvious errors. However, sometimes it trips over something, either due to a bug or because it did not manage to obtain all the information it needs. When this happens, its output is not so (Intelli)sensible.
I would simply ignore the IntelliSense error, or, like you said, use auto instead of item* if you want to get rid of the error. Maybe in a later update, or after changes to your code, the error will magically disappear.

Has casting (using as) in Swift changed recently (maybe in the latest beta)?

I've just gone back to a project I started ages ago and the compiler is throwing out hundreds of errors about objects not being convertible (to objects which subclass them), and suggesting I use as! instead of as, to force the cast.
Is this a bug in the latest X-Code beta, or has the down-casting syntax changed?
The syntax has changed for Swift 1.2. See Apple's blog post about it.
Here's a summary from their article:
Swift 1.2 separates the notions of guaranteed conversion and forced
conversion into two distinct operators. Guaranteed conversion is still
performed with the as operator, but forced conversion now uses the as!
operator. The ! is meant to indicate that the conversion may fail.
This way, you know at a glance which conversions may cause the program
to crash.

Xcode indentation with Enums

I think several people has experience this problem, and that's with enums.
So the problem is quite simple, Xcode handles indentation after enums quite strange, at first I thought it was my syntax, but it turns out that it's probably not (I'm not ruling that out just yet)
So here's what my code look like:
enum Signs : bool {
Positive = true,
Negative = false
};
<This is where Xcode suggest where my next line of code should be, which is one tab more then I expect.>
Anyone seen this problem and solved it?
This bug can be replicated 100% of the time by specifying a value for one of the enum members.
If you omit the = true, automatic indentation will perform correctly. This goes for any enum with any number of enum items. As long as one item has = someValue, the closing brace will be misaligned.
it's 2018 and the developers of XCode still haven't fixed this bug in Xcode 9.2. So, please file bug reports (Help menu -> Report an Issue), now that the method for reproducing it every time has been discovered.
I usually write my enums like this:
//Using your example
typedef enum {
kPositive,
kNegative
}signs;
There you won't have the indentation problem. But I can't answer why Xcode behaves like that.

taking the address of temporary object

I'm developping on many platforms, today I have a problem with iOS and xCode,
I'm upadating some projects to the last xCode 4.3.2 (Apple LLVM compiler 3.1)
since few time a warning has become an error: "taking the address of temporary object"
unfortunately I used many of that, see my example :
float dist = Vector3Dlength(&Vector3D(pos2 - pos1));
to avoid to create a temporary var and produce a new line of code (although this one is created on the stack by the compiler)
I know the mistakes that can lead since 10 years of coding like that :) but I WANT to continue like that...
someone have a suggestion to avoid this error without having to edit the code ? (with the new xCode 4.3.2 (Apple LLVM compiler 3.1))
You'll need to change your code, you can't take an address of something that is not an lvalue, and that temporary isn't one.
Change your code to take a const reference to Vector3D instead. This will not cost you a copy, and is well-defined behavior.
float Vector3Dlength(Vector3D const& pvect) {
return sqrt(pvect.x * pvect.x ...);
}
...
float dist = Vector3Dlength(Vector3D(pos2 - pos1));

Resources