The left operand of '>' is a garbage value - iphone

When i build and anylayse my app , i get the below memeory warning
"The left operand of '>' is a garbage value" at this line isTrue = (newLength > 20) ? NO : YES;
What is the problem over here.Thanks for any help
(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger newLength;
BOOL isTrue;
if(textField == txtFirstName){
newLength = [textField.text length] + [string length] - range.length;
//isTrue = (newLength > 20) ? NO : YES;
}
if(textField == txtLastName){
newLength = [textField.text length] + [string length] - range.length;
//isTrue = (newLength > 20) ? NO : YES;
}
if(textField == txtEmail){
newLength = [textField.text length] + [string length] - range.length;
//isTrue = (newLength > 100) ? NO : YES;
}
if(textField == txtCompanyName){
newLength = [textField.text length] + [string length] - range.length;
//isTrue = (newLength > 30) ? NO : YES;
}
if(textField == txtPassword){
newLength = [textField.text length] + [string length] - range.length;
//isTrue = (newLength > 30) ? NO : YES;
}
if(textField == txtRe_EnterPassword){
newLength = [textField.text length] + [string length] - range.length;
//isTrue = (newLength > 30) ? NO : YES;
}
if(textField == txtZipCode){
newLength = [textField.text length] + [string length] - range.length;
//isTrue = (newLength > 20) ? NO : YES;
}
if(textField == txtCountry){
newLength = [textField.text length] + [string length] - range.length;
//isTrue = (newLength > 30) ? NO : YES;
}
isTrue = (newLength > 20) ? NO : YES;
return isTrue;
}

The problem occurs when all of the if conditions fail, ie. if textField is not one of the set you are expecting it to be.
In this case, newLength is never assigned to, and will be a random garbage value.
Whilst this is probably "never going to happen", the static analyzer is not aware of that.
You should initialize newLength to have a default value, perhaps zero.

the analyzer cannot confirm that your 'switch-like construct' does not 'fall through'. newLength's value may never be set if all those if cases fail. then in that event, it would qualify as a garbage value.

I have made some changes to your code I think now it would be work better.
(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger newLength = 0;
BOOL isTrue = false;
if(textField == txtFirstName){
newLength = [textField.text length] + [string length] - range.length;
isTrue = (newLength > 20) ? NO : YES;
}
else if(textField == txtLastName){
newLength = [textField.text length] + [string length] - range.length;
isTrue = (newLength > 20) ? NO : YES;
}
else if(textField == txtEmail){
newLength = [textField.text length] + [string length] - range.length;
isTrue = (newLength > 100) ? NO : YES;
}
else if(textField == txtCompanyName){
newLength = [textField.text length] + [string length] - range.length;
isTrue = (newLength > 30) ? NO : YES;
}
else if(textField == txtPassword){
newLength = [textField.text length] + [string length] - range.length;
isTrue = (newLength > 30) ? NO : YES;
}
else if(textField == txtRe_EnterPassword){
newLength = [textField.text length] + [string length] - range.length;
isTrue = (newLength > 30) ? NO : YES;
}
else if(textField == txtZipCode){
newLength = [textField.text length] + [string length] - range.length;
isTrue = (newLength > 20) ? NO : YES;
}
else if(textField == txtCountry){
newLength = [textField.text length] + [string length] - range.length;
isTrue = (newLength > 30) ? NO : YES;
}
else {
isTrue = (newLength > 20) ? NO : YES;
}
return isTrue;
}

Your code might be correct, if (at least) one of your if tests is always guaranteed to pass. But the analyzer cannot determine that. If you can convince yourself of that, you can ignore the warning.
But a better idea would be to change those into else ifs, and put one last else, inside which is an NSAssert.

Try
[a compare:b] returns -1 if a < b, 0 if a == b and 1 if a > b.
isTrue = ([newLength compare:20]==1) ? NO : YES;

Related

how to validate textfield that allows only single decimal point in textfield?

I need to restrict user to enter only two digit after decimal point. I have achieved this by following code in textfield delegate shouldChangeCharactersInRange. But its allowing to enter more than one dot. how to restrict this? Thanks in advance.
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSArray *sep = [newString componentsSeparatedByString:#"."];
if([sep count]>=2)
{
NSString *sepStr=[NSString stringWithFormat:#"%#",[sep objectAtIndex:1]];
NSLog(#"sepStr:%#",sepStr);
return !([sepStr length]>2);
}
return YES;
The best way is to use Regular Expression in shouldChangeCharactersInRange: delegate method like this
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString *newStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSString *expression = #"^([0-9]*)(\\.([0-9]+)?)?$";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression
options:NSRegularExpressionCaseInsensitive
error:nil];
NSUInteger noOfMatches = [regex numberOfMatchesInString:newStr
options:0
range:NSMakeRange(0, [newStr length])];
if (noOfMatches==0){
return NO;
}
return YES;
}
After implementing this valid strings are:
12.004546
4546.5456465
.5464
0.454
So on....
You can also restrict number of integer after decimal by using this Regular Expression#"^([0-9]*)(\\.([0-9]{0,2})?)?$"
After implementing this valid strings are:
12.00
4546.54
.54
0.45
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString *sepStr;
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSArray *sep = [newString componentsSeparatedByString:#"."];
if([sep count]>=2)
{
sepStr=[NSString stringWithFormat:#"%#",[sep objectAtIndex:1]];
NSLog(#"sepStr:%#",sepStr);
if([sepStr length] >2)
{
return NO;
}
else
{
return YES;
}
}
return YES;
}
When a dot is entered, you should check whether a dot is present already, and return NO if it is present.
NSString * newString = [textField.text stringByReplacingCharactersInRange: range withString: string];
NSArray * sep = [newString componentsSeparatedByString: #"."];
if([string isEqualToString:#"."] && [sep count] > 1){
//already a . is there.. so don't allow new one
return NO;
}
if ([sep count] >= 2) {
NSString * sepStr = [NSString stringWithFormat: #"%#", [sep objectAtIndex: 1]];
NSLog(#"sepStr:%#", sepStr);
return !([sepStr length] > 2);
}
return YES;
Updated answer for Swift 3 using the reg ex "^([0-9]*)(\\.([0-9]+)?)?$":
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let newText = ((textField.text ?? "") as NSString).replacingCharacters(in: range, with: string)
let expression = "^([0-9]*)(\\.([0-9]+)?)?$"
guard let regex = try? NSRegularExpression(pattern: expression, options: .caseInsensitive) else {
return false
}
let noOfMatches = regex.numberOfMatches(in: newText, options: [], range: NSMakeRange(0, newText.characters.count))
return noOfMatches > 0
}
You can use this method:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSRange temprange = [textField.text rangeOfString:#"."];
if ((temprange.location != NSNotFound) && [string isEqualToString:#"."])
{
return NO;
}
return YES;
}
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSArray *sep = [newString componentsSeparatedByString:#"."];
if([string isEqualToString:#"."]){
if([textField.text containsString:#"."]){
return NO;
}
}
if([sep count] >= 2)
{
NSString *sepStr=[NSString stringWithFormat:#"%#",[sep objectAtIndex:1]];
return !([sepStr length]>2);
}
return YES;
}

UITableViewCell make phone number style

In iPhone contacts tableview there is a cell called "phone", when user edit this cell phone number becomes in something like this: (251) 575-3621. How can I make this cell type?
Thanks..
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
NSUInteger currentLength = textField.text.length;
NSCharacterSet *numbers = [NSCharacterSet decimalDigitCharacterSet];
if (range.length == 1) {
return YES;
}
if ([numbers characterIsMember:[string characterAtIndex:0]]) {
if ( currentLength == 3 )
{
if (range.length != 1)
{
NSString *firstThreeDigits = [textField.text substringWithRange:NSMakeRange(0, 3)];
NSString *updatedText;
if ([string isEqualToString:#"-"])
{
updatedText = [NSString stringWithFormat:#"%#",firstThreeDigits];
}
else
{
updatedText = [NSString stringWithFormat:#"%#-",firstThreeDigits];
}
[textField setText:updatedText];
}
}
else if ( currentLength > 3 && currentLength < 8 )
{
if ( range.length != 1 )
{
NSString *firstThree = [textField.text substringWithRange:NSMakeRange(0, 3)];
NSString *dash = [textField.text substringWithRange:NSMakeRange(3, 1)];
NSUInteger newLenght = range.location - 4;
NSString *nextDigits = [textField.text substringWithRange:NSMakeRange(4, newLenght)];
NSString *updatedText = [NSString stringWithFormat:#"%#%#%#",firstThree,dash,nextDigits];
[textField setText:updatedText];
}
}
else if ( currentLength == 8 )
{
if ( range.length != 1 )
{
NSString *areaCode = [textField.text substringWithRange:NSMakeRange(0, 3)];
NSString *firstThree = [textField.text substringWithRange:NSMakeRange(4, 3)];
NSString *nextDigit = [textField.text substringWithRange:NSMakeRange(7, 1)];
[textField setText:[NSString stringWithFormat:#"(%#) %#-%#",areaCode,firstThree,nextDigit]];
}
}
}
else {
return NO;
}
return YES;
}

How to restrict user to type in textfield after typing some characters

I am using textfield in my application and I want to restrict user typing only 15 characters in the textfield. After that he/she should not be able to type in the textfield.
How can I set this kind of functionality?
There's a bit of a trick to this, you need to calculate what the new string will be before you can test whether to allow or deny the change
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if ([newString length] > 15) {
return FALSE;
} else {
return TRUE;
}
}
//its big code but working fine for me
//put Your Text Field Name instead of YourTextFieldName in this code
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField==YourTextFieldName)
{
NSString *text = nil;
int MAX_LENGTH = 12;
text = YourTextFieldName.text;
if ([text length] <= 7)
{
NSString *separator = #"-";
int seperatorInterval = 3;
NSString *originalString = [textField.text stringByReplacingOccurrencesOfString:separator withString:#""];
if (![originalString isEqualToString:#""] && ![string isEqualToString:#""])
{
NSString *lastChar = [YourTextFieldName.text substringFromIndex:[YourTextFileName.text length] - 1];
int modulus = [originalString length] % seperatorInterval;
if (![lastChar isEqualToString:separator] && modulus == 0)
{
YourTextFieldName.text = [YourTextFieldName.text stringByAppendingString:separator];
}
}
}
if ([text length] > 7)
{
NSString *separator = #"-";
int seperatorInterval = 6;
NSString *originalString = [textField.text stringByReplacingOccurrencesOfString:separator withString:#""];
if (![originalString isEqualToString:#""] && ![string isEqualToString:#""])
{
NSString *lastChar = [YourTextFieldName.text substringFromIndex:[YourTextFieldName.text length] - 1];
int modulus = [originalString length] % seperatorInterval;
if (![lastChar isEqualToString:separator] && modulus == 0)
{
YourTextFieldName.text = [YourTextFieldName.text stringByAppendingString:separator];
}
}
}
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > MAX_LENGTH) ? NO : YES;
}
return YES;
}
You can check if typing and count chars
(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (range.length > 15) {
// delete
}
else
{
// add
}
}
use method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (([textField.text length] - range.length) == 15) {
return NO;
}
return YES;
}
hope it helps. happy coding :)
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.text.length >= 15)
{
return NO; //return NO to not change text
}
return YES;
}

textField:shouldChangeCharactersInRange:replacementString:

How can I correct this code. I want only numbers and range should be not exceed to 10.
My code is
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 10) ? NO : YES;
static NSCharacterSet *charSet = nil;
if(!charSet) {
charSet = [[[NSCharacterSet characterSetWithCharactersInString:#"0123456789"] invertedSet] retain];
}
NSRange location = [string rangeOfCharacterFromSet:charSet];
return (location.location == NSNotFound);
}
The problem here is that anything after the first return is not executed.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 10) ? NO : YES;
// unreachable!
So you are just checking the length but not whether the input is numerical. Change this line:
return (newLength > 10) ? NO : YES;
with this one:
if (newLength > 10) return NO;
and it should work. You can also optionally change this:
[NSCharacterSet characterSetWithCharactersInString:#"0123456789"]
with this:
[NSCharacterSet decimalDigitCharacterSet]

Backspace not working when implementing shouldChangeCharactersInRange method - iPhone Dev

Problem... I have a string of allowable characters "0123456789." How do I also allow the backspace from the keyboard... when I implement the code from below... the backspace key no longer works... How can I fix this?
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSCharacterSet *nonNumberSet = [[NSCharacterSet characterSetWithCharactersInString:#"0123456789."] invertedSet];
return ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0);
}
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSCharacterSet *nonNumberSet = [[NSCharacterSet characterSetWithCharactersInString:#"0123456789."] invertedSet];
if (range.length == 1){
return YES;
}else{
return ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0);
}
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ([string length] == 0)
return YES;
NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
string = [[string componentsSeparatedByCharactersInSet:nonNumberSet] componentsJoinedByString:#""];
textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
return NO;
}
This should work correctly with deleting/cutting multiple characters at once, as well as pasting. Corrections welcome. The only known problem is that when you edit in the middle of the text field the cursor gets sent to the end (because it returns NO) -- I guess you have to use a UITextView to fix that.
NSCharacterSet *theNonNumberSet = [[NSCharacterSet characterSetWithCharactersInString:#"0123456789."] invertedSet];
if (range.length == 1){
return YES;
}else if(textField.text.length < ZipcodeTextLength)
{
return ([string stringByTrimmingCharactersInSet:theNonNumberSet].length > 0);
}
else
return NO;
This will allow Numbers and Backspace and also you can limit the text length.
This is one of my implementations. Maybe it works for you.
-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
for(UIView *view in _chooseUsernameDialog.subviews) {
if([view isKindOfClass:[UIButton class]]) {
int realLength;
const char * _char = [string cStringUsingEncoding:NSUTF8StringEncoding];
int isBackSpace = strcmp(_char, "\b");
if (isBackSpace == -8) {
// is backspace
realLength = [textField.text length] - 1 ;
}
else
{
realLength = [textField.text length] + 1;
}
NSLog(#"%d", realLength );
if(realLength < 4)
{
//too short
}
else{
//long enough
}
}
}
return !([[textField text] length] + (string.length - range.length) > 13);
}