Xcode 13 SwiftUI Preview does not support Text operator + - swiftui

Seems Xcode 13 SwiftUI does not support + operator. For example
Text("Not you? Hit the")
+ Text(" ‘Back’ ").fontWeight(.bold)
+ Text("arrow and use a different email.")
Because of an error of
ambiguous operator declarations found for operator
----------------------------------------
CompileDylibError: Failed to build WarningRedirectToLoginView.swift
Compiling failed: ambiguous operator declarations found for operator
/Users/liang.wang.cm/Documents/Project/Demo-IOS/Demo/View/Login/WarningRedirectToLoginView.swift:23:192: error: ambiguous operator declarations found for operator
Text(__designTimeString("#6206.[1].[2].property.[0].[0].arg[0].value.[0].arg[2].value.[1].arg[0].value.[0].[0]", fallback: "Looks like you already have an account for ")) +
^
Swift.:1:16: note: found this matching operator declaration
infix operator + : AdditionPrecedence
^
Demo_Dev.:1:16: note: found this matching operator declaration
infix operator + : DefaultPrecedence
^

you can add Text together in SwiftUI. Here is an example that works for me: make sure the spacing is correct, (blank space (or more) before + blank space (or more) after).
You can also use a HStack
struct ContentView: View {
var body: some View {
Text("Not you? Hit the") + Text(" ‘Back’ ").fontWeight(.bold) + Text("arrow and use a different email.")
HStack {
Text("text1").foregroundColor(.green)
Text(" text2 ").foregroundColor(.blue)
Text("text3").foregroundColor(.red)
}
}
}
Note the error seems to indicate that you have declared a + operator for your own purpose. The compiler may be confused about that.

Related

SwiftUI conditional .buttonStyle

I am trying to change the style of a button based on a condition like this:
.buttonStyle((selection == index) ? .borderedProminent : .bordered)
Strangely it throws this error:
Type 'ButtonStyle' has no member 'borderedProminent'
I suppose I am making a syntax mistake?

ViewBuilder not building, functionality not working

Getting an error: Cannot convert value of type '_ConditionalContent<Text, Text>' to specified type ().
This seems like an error with the latest update.. however I have other ViewBuilders in my code that I am afraid to touch now. I honestly can't see how to make a ViewBuilder simpler than this and it won't build.
**Update: Thanks for the answer, needed to add a return type, not a bug
#ViewBuilder func positiveOrNot(x: Int) {
if x > 0 {
Text("Positive")
} else {
Text("Negative")
}
}

Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'T'

I try to compile the following code:
class CFileOperations
{
...
template <typename T>
inline void load_and_save_data(std::fstream* stream, T& value, const EOperation operation)
{
switch (operation) {
case EOperation::OpSave:
*stream << value; <-- here
break;
case EOperation::OpLoad:
*stream >> value; <-- and here
break;
}
}
...
};
I get the following errors:
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'T' (or there is no acceptable conversion)
Error C2679 binary '>>': no operator found which takes a right-hand operand of type 'T' (or there is no acceptable conversion)
For example, I use it this way, with number being an 'int':
this->load_and_save_data(stream, number, operation);
I'm using Visual C++ 2019.
What's the root cause, and how to solve it. Any idea ?
My bad, one of the calls was with a 'class enum'. Of course, >> and << are not defined for it.
For #cdhowie, here are two examples of the resulting simplicity (with the help of load_and_save_data template methods):
Here mMembers is a std::unorderedmap (cf. save_and_load_data in the question above, I have also one for the starndard containers):
void CHexArea::load_and_save()
{
this->load_and_save_data((char&)mColor);
this->load_and_save_data(mTouchLeft);
this->load_and_save_data(mTouchRight);
this->load_and_save_data(mTouchBottom);
this->load_and_save_data(mTouchTop);
this->load_and_save_data(mMembers);
}
Here, in preferences, there are two versions of files:
void CHexPreferences::load_and_save()
{
if( this->is_loading() ) {
this->reset(); // version's forward compatibility
}
int version = 2;
this->load_and_save_data(version);
this->load_and_save_data(mBoardOrientation);
this->load_and_save_data(mBoardSize);
this->load_and_save_data(mComputerStarts);
this->load_and_save_data(mComputerInitialTurns);
if( version >= 2) {
this->load_and_save_data(mComputerTilesPerTurn);
}
this->load_and_save_data(mDebugFlags);
}
Simple and clear.
Of course, there are two methods (load() and save()) that are the outer interface and calls those here above, but: 1. They are part of a library (no need to rewrite them, OO as usual) and 2. The core of the load/save is written only once in load_save_data, with the advantage of simplicity, and having corresponding load and save code (types, order...).
Of course, there are cons, but I hope you'll see that it may make sense for some people to think that there are (IMHO very strong) pros as well.
The rest is a matter of taste.

Overloaded operator has too many parameters, visual studio c++

I'm trying to declare an overload, non-friend, non-member ' - - operator in a header file:
Quad operator-(const Quad &qu1, const Quad &qu2);
But I am getting:
"error C2804: binary 'operator -' has too many parameters"
This code is right from the book and problem statement and I cannot seem to resolve it. Thanks for your help.
Binary operators in class definition scope must take only one argument.
Quad operator-(const Quad &quRight)
{
Quad res;
res.x = this->x - quRight.x;
// all other components
// ...
return res;
}
Or you can move operator overloading outside of class.

Replacing a set object with a new set object

I have a class with a private field:
std::set<std::string> _channelNames;
.. and an optional setter function:
void setChannelNames(std::set channelNames);
In the setter function, how do I replace the private _channelNames field with the one passed from the setter function?
I tried:
void Parser::setChannelNames(std::set channelNames) {
this->_channelNames = channelNames;
}
But this produced an error in VS2005:
Error 2 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::set' (or there is no acceptable conversion) parser.cpp 61
I am definitely a C++ novice, and expect that I should be doing some pointer work here instead.
Any quick tips?
Thanks!
You just have to specialize template. You cannot use std::set without specialization.
void Parser::setChannelNames(const std::set<std::string> & channelNames) {
this->_channelNames = channelNames;
}