appending objectForKey title after changing it with user input

Discussion in 'iOS Programming' started by gbenna, Mar 20, 2012.

  1. gbenna macrumors member

    Joined:
    Jul 27, 2011
    #1
    I am saving a NSMutableArray with different objectsForKeys.
    I am having Three of the object be set by input by the user.
    I would like to be able to append the new object text with .caf and .txt. So when these are called in the next viewController they can be recognized and used.

    Here is my code:

    Code:
    - (void)saveAction:(id)sender {
        
        [self.nameTextField resignFirstResponder];
        [self.imageNameTextField resignFirstResponder];
        
        NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                           
                                           
                                           @"main.caf",@"pictureAudioKey",
                                           @"audio1.m4a",@"firstAudioKey",
                                           @"audio2.m4a",@"secondAudioKey",
                                           @"audio3.m4a",@"thirdAudioKey",
                                           @"audio4.m4a",@"fourthAudioKey",
                                           @"audio5.m4a",@"fifthAudioKey",
                                           @"audio6.m4a",@"sixthAudioKey",
                                           @"main.jpg",@"photoimagekey",
                                           @"some.txt", @"identity", 
                                           @"imagename.txt",@"numberkey",nil];
    
     //here is where I change the object values by what the user puts into   the textfield imagename.text 
    This works fine especially for the photo since it is recognized as a png without the extension and loaded 
        [dictionary setObject:[[self imageNameTextField]text]
                       forKey:@"photoimagekey"];
    //However when you strip the .txt from a text file...or the .caf from an audio //file they are not recognized and not loaded.
        [dictionary setObject:[[self imageNameTextField]text]
                       forKey:@"identity"];
    //I tried this
    //identity=[identity stringByAppendingFormat:@"txt" But it didn't work
    
        NSString *audioString = [dictionary objectForKey:@"pictureAudioKey"];
        audioString = [audioString stringByAppendingFormat:@".caf"];
        [dictionary setObject:[[self imageNameTextField]text]
                       forKey:@"pictureAudioKey"];
    
    Does anyone have any suggestions?
     
  2. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    You post isn't making much sense to me.

    Your first sentence is wrong where you say "NSMutableArray".

    You mention three objects, but it looks like you are creating those from one input field, so I kind of question the need for that.

    You mention you want to append to the dictionary, but you are in fact overriding values for the three keys pictureAudioKey, photoimagekey, and identity. If your code actually looks like this, then don't bother adding those values to begin with, or do an actual append by using different key names.

    You set the initial value for photoimagekey with a .jpg extension but later mention that by not using an extension that the file will load a .png. Make up your mind what your using.

    Unless the user typed in the extension, which your code suggests that they shouldn't, you should be appending the .caf and .txt to the input result before appending or altering the dictionary.

    In the last few lines of code, you are extracting the value of pictureAudioKey, which is "main.caf", altering by adding .caf, and then doing nothing with that result. I expect the result of the alteration to be "main.caf.caf".

    What I think you might want are lines that look something like this;
    Code:
    [dictionary setObject:[[[self imageNameTextField] text] stringByAppendingFormat:@".txt"] forKey:@"identity"];
    [dictionary setObject:[[[self imageNameTextField] text] stringByAppendingFormat:@".caf"] forKey:@"pictureAudioKey"];
    [dictionary setObject:[[[self imageNameTextField] text] stringByAppendingFormat:@".jpg"] forKey:@"photoimagekey"];
    
    I'll say it again, if you want to append values, then you need new key names because these overwrite the values you initialized for the keys.

    Since I see no retains or releases I am assuming you are using ARC. The biggest hint to that is your use of class method dictionaryWithObjectsAndKeys: which returns a non-retained object. I believe ARC will retain it for you.

    Oh, you have declared dictionary as a local variable, so how are you getting that to the next view controller or otherwise saving it?

    Your code is incomplete and that makes it difficult for others help you.
     
  3. dejo, Mar 21, 2012
    Last edited: Mar 21, 2012

    dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    gbenna, I gotta ask: What have you done to educate yourself in the fundamentals of Objective-C and iOS programming? Please be specific.

    The reason I ask is that you seem to be lacking in this area and you appear to be just throwing code together, without understanding what it does often, hoping that it will work. I'm not saying that there isn't an element of trial-and-error in programming but without a solid foundation of the basics, you are not setting yourself on the path to success and are spending much more time than needed to solve problems.

    P.S.
    You're still not using stringByAppendingFormat: properly. Besides, for what you're doing, there's another NSString instance method for appending a string that would work better.
     

Share This Page