Question

I need to save profile searches and allow the user to click them instead of typing the email address again. I am putting the most recent search at the top of the list and trying to not let them duplicate. For some reason It works the first few clicks then duplicates them. Can anyone suggest cleaner and more correct code to achieve this?

enter image description here

If you click on a recent email it will search that one and move it to the top. For some reason sometimes it leaves a copy at the bottom and sometimes it works okay.

@implementation Search
{
bool badInfo;
bool first;
bool second;
bool third;
bool fourth;
bool fifth;
NSInteger success;
NSString *firstemail;
NSString *secondemail;
NSString *thirdemail;
NSString *fourthemail;
NSString *fifthemail;
NSString *searchEmail;
}

- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

firstemail = [defaults objectForKey:@"firstemail"];
secondemail = [defaults objectForKey:@"secondemail"];
thirdemail = [defaults objectForKey:@"thirdemail"];
fourthemail = [defaults objectForKey:@"fourthemail"];
fifthemail = [defaults objectForKey:@"fifthemail"];
_recentEmailOne.enabled = NO;
_recentEmailTwo.enabled = NO;
_recentEmailThree.enabled = NO;
_recentEmailFour.enabled = NO;
_recentEmailFive.enabled = NO;

if (firstemail) {
    _recentEmailOne.enabled = YES;
    [_recentEmailOne setTitle:firstemail forState:UIControlStateNormal];
}
if (secondemail) {
    _recentEmailTwo.enabled = YES;
    [_recentEmailTwo setTitle:secondemail forState:UIControlStateNormal];
}
if (thirdemail) {
    _recentEmailThree.enabled = YES;
    [_recentEmailThree setTitle:thirdemail forState:UIControlStateNormal];
}
if (fourthemail) {
    _recentEmailFour.enabled = YES;
    [_recentEmailFour setTitle:fourthemail forState:UIControlStateNormal];
}
if (fifthemail) {
    _recentEmailFive.enabled = YES;
    [_recentEmailFive setTitle:fifthemail forState:UIControlStateNormal];
}
_emailInput.text = @"";
_emailInput.delegate = self;


}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.



}

- (IBAction)emailOne:(id)sender {
searchEmail = firstemail;
[self doSearch];
}

- (IBAction)emailTwo:(id)sender {
searchEmail = secondemail;
[self doSearch];

}

- (IBAction)emailThree:(id)sender {
searchEmail = thirdemail;
[self doSearch];

}

- (IBAction)emailFour:(id)sender {
searchEmail = fourthemail;
[self doSearch];

}

- (IBAction)emailFive:(id)sender {
searchEmail = fifthemail;
[self doSearch];

}

And in the search results where I save the information. This is where the problem must be.

//save data
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:searchEmail forKey:@"firstemail"];

if (fifthemail) {
    if ([fifthemail isEqualToString:searchEmail]) {
        fifthemail = nil;
    } else {
        fifth = YES;
    }
}

if (fourthemail) {
    if ([fourthemail isEqualToString:searchEmail]) {
        fourthemail = nil;
    } else {
        fourth =  YES;
    }
}

if (thirdemail) {
    if ([thirdemail isEqualToString:searchEmail]) {
        thirdemail = nil;
    } else {
        third = YES;
    }
}

if (secondemail) {
    if ([secondemail isEqualToString:searchEmail]) {
        secondemail = nil;
    } else {
        second = YES;
    }
}

if (firstemail) {
    if ([firstemail isEqualToString:searchEmail]) {
        firstemail = nil;
    } else {
        first = YES;
    }
}


if (first) {
   [defaults setObject:firstemail forKey:@"secondemail"];
} else if (second) {
   [defaults setObject:secondemail forKey:@"secondemail"];
   second = NO;
}
if (second) {
    [defaults setObject:secondemail forKey:@"thirdemail"];
} else if (third) {
    [defaults setObject:thirdemail forKey:@"thirdemail"];
    third = NO;
} else if (fourth) {
    [defaults setObject:fourthemail forKey:@"thirdemail"];
    fourth = NO;
}

if (third) {
    [defaults setObject:thirdemail forKey:@"fourthemail"];
} else if (fourth) {
    [defaults setObject:fourthemail forKey:@"fourthemail"];
    fourth = NO;
} else if (fifth) {
    [defaults setObject:fifthemail forKey:@"fourthemail"];
    fifth = NO;
}

if (fourth) {
    [defaults setObject:fourthemail forKey:@"fifthemail"];
} else if (fifth) {
    [defaults setObject:fifthemail forKey:@"fifthemail"];
}


if (success == 1) // good login
{
    NSLog(@"Login Success");
    [defaults setObject:searchEmail forKey:@"searchemail"];
    [defaults synchronize];
    [self performSegueWithIdentifier:@"viewprofile" sender:self];

} else if (success == 2) {
    NSLog(@"profile not found");

    [defaults setObject:searchEmail forKey:@"searchemail"];
    [defaults synchronize];
    [self performSegueWithIdentifier:@"notfound" sender:self];
} else {
    NSLog(@"bad information entered");
    NSString *error_msg = (NSString *) [jsonData objectForKey:@"message"];
    [self alertStatus:error_msg :@"Search Failed!"];

}
Was it helpful?

Solution

The most naive approach (well not counting yours) is something like this:

// Replace newString if found, insert new into top
int match = -1;
int index = 0;
for(NSString * string in historyArray)
{
    if([newString isEqualToString:string])
    {
        match = index;
        break;
    }
    index++;
}
if(match != -1)
    [historyArray removeObjectAtIndex:index];
[historyArray insertObject:newString atIndex:0];

If you then want to limit the size of the history something like this works after the above code:

if(historyArray.count > kSearchHistorySize)
    [historyArray removeLastObject];

The above only works if you add one string at a time, otherwise replace the 'if' with 'while' perhaps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top