I get the warning Comparison of unsigned expression >=0 is always true
In this line:return status != nil && status.length >= 0 && status.length <= 140;
So I am not sure if this sounds stupid or not but should I just delete that expression then>
Thanks!
Remove just this part of a check — status.length >= 0. Actually this part of a check will be optimized by compiler and removed automatically as it not make sense. Removing it form a code will help you to get rid from this warning.
Yes - since length returns an unsigned integer, it will always be 'greater than or equal to 0'. You can delete the status.length >= 0 portion of the conditional.
If you want to make sure that you don't have a zero-length string, then you should use the condition status.length > 0.
Otherwise, you can remove that condition, as others have mentioned.
You must have written a statement which can never be executed
I am having this because i do something like this..
if (textField.text.length < 0) //wrong logic
textField's text length can never be less than 0 . It must be at least 0.
Whenever complier see such kind of logical error which can never be possible (executed) . It generates this kind of Warning. If anyone is having this error so properly check your logic .
Related
The answer chosen here states that it checks if the array is not empty. And if it is not empty, that means the code should be executed.
How to check if array is null or empty?
However, I am seeing the opposite. When I use if (!array.count) with a non empty array, the code within is not executed. If I use if (array.count) with a non empty array, the code within is executed. What am I missing here?
The code in that answer is correct for the question being asked. It's the text in the answer that is confusing. The goal of that question is to see if an array is "empty". The accepted answer has the code:
if (!array || !array.count){
}
The text in that answer states:
That checks if array is not nil, and if not - check if it is not empty.
But that if statement actually means "if the array is nil or the array count is zero, do something". So it's the exact opposite.
So the code is correct but the text is wrong (or at least misleading).
Back to your own question.
if (array.count) {
// same as array.count != 0
// array is not empty (non-zero count)
}
or:
if (!array.count) {
// same as !(array.count != 0) or array.count == 0
// array is empty (zero count)
}
Remember, in C, C++, Objective-C, an if statement is treated as "true" if the expression evaluates to a non-zero value and "false" is it evaluates to zero. And the use of ! at the start negates the "true" to "false" or the "false" to "true".
I have defined a macro like this,
#define SELECTED_SITE_ID (SITE_MANAGER.selectedSite.siteCollectionIdentifier)
It's returning a double value which was stored in user defaults.
In code, SELECTED_SITE_ID macro is using for more than 1000 places like this,
int a = SELECTED_SITE_ID;
NSArray *array = [someClassObject objectAtIndex:a-1];
As my app is running for the first time, SELECTED_SITE_ID macro is returning 0.0, that's assigned to int a; so a will be 0.
Where from array, I have written a-1 to 0-1, this leads to a crash issue.
I don't know what's the quick way to fix this for now, as it's written at approx. 1000 places?
What I think?
I guess inside macro itself? If I would able to check, what's the value coming? if it's 0 then I will explicitly return 1.
Any help would be appreciated.
That was easy,
#define SELECTED_SITE_ID ((SITE_MANAGER.selectedSite.siteCollectionIdentifier <= 0.0) ? 1 : SITE_MANAGER.selectedSite.siteCollectionIdentifier)
Better way is:
#define SELECTED_SITE_ID (SITE_MANAGER.selectedSite.siteCollectionIdentifier? :1)
I found this code on another thread.
def is_number? string
true if Float(string) rescue false
end
Instead of using a method to return true or false, I'd like to do this "is_numeric" test in one line, in an if statement. Can someone explain if this is possible? I'm getting errors at the moment both when the string variable is null and when it contains non-numeric characters.
if Float(string)
* do something
else
* do something else
end
if Float() is pointless code, since Float() will either return a truthy value or raise an error (based on my limited look at the source code - as of writing, you can follow the code path from line #2942). I'd suggest you're asking the wrong question/looking at the problem wrong (it'd be helpful to know what you're actually trying to achieve).
To do something with Float() on one line and avoid breaking code, use rescue as a statement modifier, as has been done in the is_number? method posted.
Float(string) rescue 0.0 # trying to emulate String#to_f!
Ensuring the phone number is 10 digits, numbers only, is quite simple.
PHONE_NUM_LENGTH = 10
string.length == PHONE_NUM_LENGTH && string.count('0-9') == PHONE_NUM_LENGTH
will return the true/false value representing this check. This is more efficient than a Regex.
The first part,
string.length == PHONE_NUM_LENGTH
checks whether the string is 10 characters long. The second,
string.count('0-9') == PHONE_NUM_LENGTH
checks whether it has exactly 10 numeric characters.
I am working on reports for a website and I am currently thinking of what would be the best way to handle BigDecimal -0.0's.
The database I'm working with has a lot of them. When these -0.0's are put through number_to_currency(), I get "$-0.00". My format for negative numbers is actually "-$x.xx", so note that number_to_currency is not formatting it as a negative number (otherwise there would also be a negative sign in front of the dollar sign), but for some reason the negative sign is being translated along with the 0.
Right now my solution is to do this every time I get an amount from the database:
amount *= -1 if amount == 0 && amount.sign == -1
This changes the -0.0 to a 0.0. It's simple enough, but I can't help but wonder if there is a better solution, or something on BigDecimals or number_to_currency to handle this situation that I'm just not finding.
That is so because the number is converted into a string to be displayed. And:
# to_d converts to BigDecimal, just FYI
"-0".to_d.to_s #=> "-0.0"
Therefore you will have to make it a 0 yourself. But the sign-checks are redundant - a simple comparison with 0 will do the trick:
bdn = "-0".to_d # or BigDecimal.new("-0")
value = bdn.zero? ? 0 : bdn
number_to_currency(value, other_options)
However, you wouldn't want to manually add this check everywhere you're calling number_to_currency. It would be more convenient to create your own modified_number_to_currency method, in your ApplicationHelper, like so:
def modified_number_to_currency( number, options )
value = number.zero? ? 0 : number
number_to_currency(value, options)
end
And then use modified_number_to_currency instead of number_to_currency.
Alternatively, you could overwrite number_to_currency and have it call super in the end. That might also work but I'm not 100% certain.
Coming to your check specifically:
amount *= -1 if amount == 0 && amount.sign == -1
It should simply be:
amount = 0.to_d if amount.zero? # the to_d might or might not be required
Does an if-statement with an && operator check for the second parameter if the first one is false / NO?
Would the following be able to crash?
NSDictionary *someRemoteData = [rawJson valueForKey:#"data"];
if( [someRemoteData isKindOfClass:[NSDictionary class]] && someRemoteData.count > 0 ){
//..do something
}
Please no simple yes or no answer, but explain why.
No, it does not evaluate the expression after learning that the answer is going to be NO. This is called short-circuiting, and it is an essential part of evaluating boolean expressions in C, C++, Objective C, and other languages with similar syntax. The conditions are evaluated left to right, making the evaluation scheme predictable.
The same rule applies to the || operator: as soon as the code knows that the value is YES, the evaluation stops.
Short-circuiting lets you guard against invalid evaluation in a single composite expression, rather than opting for an if statement. For example,
if (index >= 0 && index < Length && array[index] == 42)
would have resulted in undefined behavior if it were not for short-circuiting. But since the evaluation skips evaluation of array[index] when index is invalid, the above expression is legal.
Objective-C uses lazy evaluation, which means that only the left operand is evaluated in your case.
NO it does not. If the first statement fails the second is never checked so for example you can do this (ArrayList != null && ArrayList.size() > 0) and you will never get an error if the variable is not initialized.