binary operator '==' cannot be applied in swift 3 [duplicate] - swift3

This question already has answers here:
Binary operator '==' cannot be applied to operands of type 'Any?' and 'String' Swift iOS
(3 answers)
Closed 5 years ago.
In Swift 3 I'm getting a error when I try to compare two items.
var userData = NSDictionary()
if !(self.userData.count == 0) && (self.userData["user_status"] == "1") {
}
The error says: Binary operator '==' cannot be applied to operands of type 'Any?; and 'String'
What is the correct way to do this in Swift 3?

You should do it like this:
var userData = [String : String]()
userData["user_status"] = "1"
if !(userData.count == 0) && (userData["user_status"] == "1") {
// do something here
print ("hello")
}

Related

How to use Regex Conditional OR statement within karate match contains? [duplicate]

This question already has an answer here:
Karate - Is there a way where I can use a variable in string regex
(1 answer)
Closed 1 year ago.
Consider below JSON. I want to match each of temp has a partial string matching 'process' using a OR condition i.e. in each temp either severity or conditionName should have a partial matching of 'process'
* def temp = [{ "severity": "Critical","conditionName": "process"}, { "severity": "Critical 2","conditionName": "process 2" }, { "severity": "Critical 2","conditionName": "processor" } ]
I tried below code:
* def isMatch = function(x) { return x.severity == '#regex (?i).*process.*' || x.conditionName == '#regex (?i).*process.*' }
* match each temp == '#? isMatch(_)'
Please note that the things like #regex etc. work only for the Karate match keyword. You are attempting to use this in JavaScript code here, which will not work.
This should achieve what you want to do:
* def isMatch = function(x) { return x.severity.includes('process') || x.conditionName.includes('process') }
You can do more complicated matches (I think even regex in JS is possible) so keep that in mind. Also note that there is a karate.match() API - which will return an object with a pass property - but it depends on a variable being available in the Karate scope: https://stackoverflow.com/a/50350442/143475

Why my regEx does not work in JScript? [duplicate]

This question already has answers here:
Differences between Javascript regexp literal and constructor
(2 answers)
Javascript RegEx Not Working [duplicate]
(1 answer)
Closed 4 years ago.
I have the following RegEx that works well in Java Script but not in JScript. The only difference I found between the 2 is the that JScript uses /expression/ and tried it with no luck. I need to match specific string date format.
var pattern = "/([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))T(00|[0-9]|1[0-9]|2[0-3]):([0-9]|[0-5][0-9]):([0-9]|[0-5][0-9])$/";
var regexpattern = new RegExp(pattern);
var str = "2018-02-28T17:05:10";
var res = regexpattern.test(str);
//var res = str.match(pattern);
if ( res != null)
{
Log.Message("Test worked ");
}
else
{
Log.Message("did not");
}
EDIT:
It should be declared as:
var pattern = /([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))T(00|[0-9]|1[0-9]|2[0-3]):([0-9]|[0-5][0-9]):([0-9]|[0-5][0-9])$/;

How to get Boolean variable in Regular expressions Swift 4 [duplicate]

This question already has answers here:
How to validate an e-mail address in swift?
(39 answers)
Closed 5 years ago.
I'm trying to make a regular expression that looks for an e-mail. Everything works . How to get a Bool variable that would mean whether such an expression was found or not?
let someString = "123milka#yandex.ru123"
let regexp = "([a-zA-Z]{1,20})#([a-zA-Z]{1,20}).(com|ru|org)"
if let range = someString.range(of: regexp, options: .regularExpression) {
let result : String = someString.substring(with: range)
print(result)
}
You already have an if test, so use that, setting your Boolean as you see fit. There are tons of ways of doing that, e.g.:
let success: Bool
if let range = someString.range(of: regexp, options: .regularExpression) {
success = true
let result = someString.substring(with: range)
print(result)
} else {
success = false
}

C++ stuck in while loop [duplicate]

This question already has answers here:
Why does non-equality check of one variable against many values always return true?
(3 answers)
Closed 6 years ago.
Hello I am doing a very simple while loop in C++ and I can not figure out why I am stuck in it even when the proper input is give.
string itemType = "";
while(!(itemType == "b") || !(itemType == "m") || !(itemType == "d") || !(itemType == "t") || !(itemType == "c")){
cout<<"Enter the item type-b,m,d,t,c:"<<endl;
cin>>itemType;
cout<<itemType<<endl;
}
cout<<itemType;
if someone can point out what I am over looking I'd very much appreciate it. It is suppossed to exit when b,m,d,t or c is entered.
Your problem is in your logic. If you look at your conditions for your while loop, the loop will repeat if the item type is not "b" or not "m" or not "d" etc. That means if your item type is "b", it is obviously not "m", so it will repeat. You want to use && instead of ||.
As other answers and comments wrote correctly your logic is wrong. Using find() would simplify your task:
std::string validCharacters( "bmdtc" );
while ( std::string::npos == validCharacters.find( itemType ) )
{
...
}
This solution is more general and easier to read. See also documentation of std::string::find
The boolean expression to exit the loop is flawed. The way it is, in order to exit the loop the itemType would have to be all those letters at the same time. Try to instead || the letters first, and then negate it:
while(!(itemType == "b" || itemType == "m" || itemType == "d" || itemType == "t" || itemType == "c")
try this
string itemType = "";
while(!(itemType == "b" || itemType == "m" || itemType == "d" || itemType == "t" || itemType == "c")){
cout<<"Enter the item type-b,m,d,t,c:"<<endl;
cin>>itemType;
cout<<itemType<<endl;
}
cout<<itemType;
you condition is always true

Else/If statement help in swift

I'm getting an error when I try to use an if/else statement in Xcode 6 with Swift. This is what I have
} else if countElements(sender.text) == 1, 2
It's telling me:
Type 'String!' does not conform to protocol '_CollectionType'
How can I compare two values on one line?
You can use "||" = "or"
} else if countElements(sender.text) == 1 || countElements(sender.text) == 2 {
}
The other answer correctly shows the way to make 2 comparisons in Swift, but this can be done in a single statement thanks to Swift's pattern matching operator. For example:
if 1...2 ~= countElements(sender.text) {
println("count is 1 or 2")
}
Basically, the provided statement is true if the number of characters in your string is inclusively between 1 and 2.
You can also use the contains global function, providing it a range and the element to check for inclusion:
contains(1...2, countElements(sender.text))