Question

I'd like to improve this method if possible: this is a small section whereby all of the textfield (eyepiece, objectivelenses etc) texts are saved. Unfortunately, having to do this lots of times for each part of my app is prone to error so I would like to improve it. I'm thinking some sort of fast enumeration with arguments for the method being the textfields etc. and I can have all the keys in a dictionary (which is already set up). Just a pointer to the right docs or, perhaps, some sort of process that has worked for you would be fantastic!

-(IBAction)saveUserEntries {


if (eyepiece.text != nil) {

    eyepieceString = [[NSString alloc] initWithFormat:eyepiece.text];
    [eyepiece setText:eyepieceString];
    NSUserDefaults *eyepieceDefault = [NSUserDefaults standardUserDefaults];
    [eyepieceDefault setObject:eyepieceString forKey:@"eyepieceKey"];
}
else {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"eyepieceKey"];
}

if (objectiveLenses.text != nil) {

    objectiveLensString = [[NSString alloc] initWithFormat:objectiveLenses.text];
    [objectiveLenses setText:objectiveLensString];
    NSUserDefaults *objectiveDefault = [NSUserDefaults standardUserDefaults];
    [objectiveDefault setObject:objectiveLensString forKey:@"objectiveKey"];
}
else {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"objectiveKey"];
}

Thank you for taking the time to read this!

Was it helpful?

Solution

I will attempt to answer this question based on a OOP solution.

Create a method that accepts whatever type object these textboxes are as an argument, send the reference of said object to the method, and save the entry in a similar method you do know. This will avoid the "copy and paste" errors you are worried about.

You should be able to loop through every instance of said object that exists, if a cocoa application, works like similar to Java and .NET ( I really don't know ). I just know there must be a way to loop through every instance of a single object within the application domain.

If this was .NET I simply would suggest TextBox.Name and TextBox.String to make this a generic method that could be used to save the properties of any TextBox sent to it. If this doesn't anwer your question ( was a little long for a comment ) then I aplogize.

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