Thanks again for the help.
I have a simple action that checks the stringValue of a textField, and if it matches - a status message prints in a second textField:
if
(textField.stringValue == (#"Whatever" )){
[outputDisplay setStringValue:#"Success"];
My question is how do I implement multiple input stringValue options in this method? For example "Whatever" "Whatever1, Whatever2" all return "Success" in the outputDisplay.
thanks.
Paul
Create a set of answers you're looking for and test if the string in question is in there.
NSSet *successStrings = [NSSet setWithObjects:#"Whatever1",
#"Whatever2",
#"Whatever3",
nil];
if ([successStrings containsObject:st]) {
[outputDisplay setStringValue:#"Success"];
}
(An array would also work, but a set is specialized for testing membership, so it's a better fit for what we're doing here.)
Firstly, to check for equality of NSString-s you should use -isEqualToString:. == compares the pointer values which often returns NO even if the two strings' contents are the same.
To check if the text field match multiple strings, you connect them with the || (or) operator, so you get
NSString* st = textField.stringValue;
if ([st isEqualToString:#"Whatever"] || [st isEqualToString:#"Whatever1"] || [st isEqualToString:#"Whatever2"]) {
[outputDisplay setStringValue:#"Success"];
Related
I'm just learning while doing ramda.js. Well, there are many ways to reach a goal with ramda, but there is on thing I do not understand.
I would like to check the input for an array of strings that all match one regular expression. I thought I could do it R.all(R.both(isString, isRegExp)), but it seems to deliver a true when the input is a number.
As expected R.allPass([isString, isRegExp]) gives a false with a number input.
But can anyone please explain me why R.all is returning a true? Or what and where is mistake (in thinking)?
Complete code:
var isString = R.is(String),
isMyRegExp = R.test(/^[a-z]+$/),
isMyRegExpString = R.both(isString, isMyRegExp),
isArrayOfMyRegExpStrings = R.all(isMyRegExpString),
isArrayOfMyRegExpStringsPass = R.allPass([isString, isMyRegExp]),
result = {
'all': isArrayOfMyRegExpStrings(9),
'allPass': isArrayOfMyRegExpStringsPass(9)
};
console.log(result);
// {
// all: true,
// allPass: false
// }
https://codepen.io/Eisenhardt/pen/PKLZqj
PS:
I know that I could shorten conditions with just the regexp, but there could be other situations where I need both conditions to be true. eg. isArrayOfNumber and sumOfNumbersOver50.
The second argument to R.all is expecting a list of values to test. Due to the way the function is implemented it is treating the 9 in your example as an empty list, resulting in a vacuous truth and evaluating to true.
I can't figure out how to build a proper search function in my Kinvey-based application. Right now I can just find values that match the text I type, and this is how:
KCSQuery* query = [KCSQuery queryOnField:#"key" withExactMatchForValue:textField.text];
[self.store queryWithQuery:query withCompletionBlock:^(NSArray *objectsOrNil, NSError *errorOrNil) {
if (errorOrNil == nil) {
[results removeAllObjects];
[results addObjectsFromArray:objectsOrNil];
[self.tableView reloadData];
}
} withProgressBlock:nil];
Hopefully we can find a solution to this. Thanks in advance.
Do you mean to fetch results that contain “key” instead of the “exact match for key”?
With KCSQuery(), you can achieve it using regular expressions like below:
let query = KCSQuery(onField: “fieldname”” usingConditional: .KCSRegex, forValue: “Regular expression” )
Also, refer http://devcenter.kinvey.com/ios/guides/datastore#Querying for better understanding of query creation using operators, modifiers etc.
If this is not the case, can you please elaborate your use case along with an example?
Thanks,
Pranav
Kinvey Support
I have a question about a string containing parentheses. Let me suppose that a string variable cityname is #"Boston (Massachusetts). I wonder why Xcode doesn't recognize the string inside parentheses? In the following code, only the first case is true.
// cityname = #"Boston (Massachusetts)"
if ([cityname rangeOfString:#"Boston"].location==!NSNotFound){
// true
}
if ([cityname rangeOfString:#"Massachusetts"].location==!NSNotFound){
// false
}
if (cityname isEqualToString:#"Boston (Massachusetts)"){
}
How can I make it so that I can validate the entire string even with parentheses (and apostrophes, too?)?
Thank you,
Tom
This has nothing to do with parentheses. You want [cityname rangeOfString:#"Massachusetts"].location != NSNotFound, not [cityname rangeOfString:#"Massachusetts"].location == !NSNotFound. They do totally different things.
How do you preset fields so that (unless a specific value is entered from the form itself) they STAY null? For another project, I will later have to pull information from this table depending on what options people choose, so if I could do a cfif against nulls I think it'd be a lot easier than the blanks that are currently generated if I don't insert any new values.
Does anyone know where/how to do this? I'm using Microsoft's SQL Server Management Studio to edit what the individual columns, and all I can find are the command codes using INSERT, SELECT, etc., rather than having a list that I edit. Or is that the only way to make my default setting be "null"?
Thanks for the help
If a field is set to be a nullable field (that is, it allows NULL), when adding a row, it will be NULL unless otherwise specified.
You don't need to do anything special for this.
If your INSERT and UPDATE statements simply omit the field, it will not be updated, though you can specifically specify NULL for such a field if wanted.
You could use a helper function to handle parameter setup. This is a simplified version of a function I use...
private static object ProcessParameter(object input)
{
if (input == null)
return input;
switch (input.GetType().ToString().ToLower())
{
case "system.string":
if (input == null || input.ToString() == "") { return DBNull.Value; }
return input;
case "system.int32":
case "system.double":
if (input.ToString() == "0" && IsNullable(input)) { return DBNull.Value; }
return input;
case "system.datetime":
if (System.Convert.ToDateTime(input) == DateTime.MinValue || System.Convert.ToDateTime(input) == default(DateTime)) { return DBNull.Value; }
return input;
default:
return input;
}
}
I got this parameter:
$objDbCmd.Parameters.Add("#telephone", [System.Data.SqlDbType]::VarChar, 18) | Out-Null;
$objDbCmd.Parameters["#telephone"].Value = $objUser.Telephone;
Where the string $objUser.Telephone can be empty. If it's empty, how can I convert it to [DBNull]::Value?
I tried:
if ([string]:IsNullOrEmpty($objUser.Telephone)) { $objUser.Telephone = [DBNull]::Value };
But that gives me the error:
Exception calling "ExecuteNonQuery" with "0" argument(s): "Failed to convert parameter value from a ResultPropertyValueCollection to a String."
And if I convert it to a string, it inserts an empty string "", and not DBNull.
How can this be accomplished?
Thanks.
In PowerShell, you can treat null/empty strings as a boolean.
$x = $null
if ($x) { 'this wont print' }
$x = ""
if ($x) { 'this wont print' }
$x = "blah"
if ($x) { 'this will' }
So.... having said that you can do:
$Parameter.Value = $(if ($x) { $x } else { [DBNull]::Value })
But I'd much rather wrap this up in a function like:
function CatchNull([String]$x) {
if ($x) { $x } else { [DBNull]::Value }
}
I don't know about powershell, but in C# I would do something like this:
if ([string]::IsNullOrEmpty($objUser.Telephone))
{
$objDbCmd.Parameters["#telephone"].Value = [DBNull]::Value;
}
else
{
$objDbCmd.Parameters["#telephone"].Value = $objUser.Telephone;
}
Always append +"" at the end of db values...
$command.Parameters["#EmployeeType"].Value= $ADResult.EmployeeType + ""
Many years later, let me clarify:
Josh's answer shows a helpful simplification for testing strings for emptiness (relying on PowerShell's implicit to-Boolean conversion[1]), but it is unrelated to Tommy's (the OP's) problem.
Instead, the error message
"Failed to convert parameter value from a ResultPropertyValueCollection to a String."
implies that it is the non-null case that caused the problem, because $objDbCmd.Parameters["#telephone"].Value expects either a string value or [DBNull]::Value, whereas $objUser.Telephone is of type [ResultPropertyValueCollection], i.e. a collection of values.
Thus, in the non-null case, a string value must be assigned, which must be derived from the collection; one option is to take the first collection element's value, another would be to join all values with a separator to form a single string, using, e.g., [string]::Join(';', $objUser.Telephone) or, if joining the elements with spaces is acceptable (not a good idea with multiple phone numbers), simply with "$($objUser.Telephone)".[2]
Detecting an empty collection via [string]:IsNullOrEmpty() actually worked, despite the type mismatch, due to how PowerShell implicitly stringifies collections when passing a value to a [string] typed method parameter.[2]
Similarly, using implicit to-Boolean conversion works as expected with collections too: an empty collection evaluates to $false, a non-empty one to $true (as long as there are either at least two elements or the only element by itself would be considered $true[1])
Therefore, one solution is to use the first telephone number entry:
$objDbCmd.Parameters["#telephone"].Value = if ($objUser.Telephone) {
$objUser.Telephone[0].ToString() # use first entry
} else {
[DBNull]::Value
}
Note: If $objUser.Telephone[0] directly returns a [string], you can omit the .ToString() call.
In PowerShell v7+ you can alternatively shorten the statement via a ternary conditional:
$objDbCmd.Parameters["#telephone"].Value =
$objUser.Telephone ? $objUser.Telephone[0].ToString() : [DBNull]::Value
[1] For a comprehensive summary of PowerShell's automatic to-Boolean conversions, see the bottom section of this answer.
[2] When implicitly converting a collection to a string, PowerShell joins the stringified elements of a collection with a single space as the separator by default; you can override the separator with the automatic $OFS variable, but that is rarely done in practice; e.g., array 'foo', 'bar' is converted to 'foo bar'; note that this conversion does not apply when you call the collection's .ToString() method explicitly, but it does apply inside expandable (interpolating) strings, e.g., "$array".