Objective-C: Problem appending strings!

Discussion in 'Mac Programming' started by Abrexas, Aug 5, 2011.

  1. Abrexas macrumors member

    Joined:
    Jul 29, 2011
    #1
    Hey guys, I'm sorry if this is a repost, or if there is a solution already on here. I looked and couldn't find anything that worked for my program.

    Here's my problem:

    I have a C Array of strings that I'm working with. (if I need this to be NSArray then I know how to do that already). What part of my program does is at a button click, a randomly generated word from the string array appears in a label. This part works fine. The problem is that I cannot figure out how to add the next new word to the label without deleting the first one.

    Code:
    - (IBAction)genWord:(id)sender
    {
    	char *list[] = {"one", "two", "three", "four", "five"};
    	int listLength = (sizeof list) / (sizeof list[0]);
    	listLength = random() % listLength;
    	
    	[oneWord setStringValue:[NSString stringWithFormat:@"%s", list[listLength]]];
    }


    I have already tried 'stringByAppendingFormat' and SEVERAL others. No luck.. =(

    Thank you for any help you can offer!

    - Steven French
     
  2. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #2
    A common trap is trying to modify an NSString instead of an NSMutableString.

    You didn't post enough of your code to tell if that is the issue.

    B
     
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #3
    What type is oneWord?

    You said "add the next new word to the label", but it's unclear what you mean by "label". Are you referring to oneWord? If so, we need to see what type it is. If you mean something else by "label", you need to show us that.


    If oneWord is a type of object that can only have an entire string set, then the logical way to "append" strings is to get the current string, make a new string composed of the current and a new string, then set that as the object's string. If you have code that does that, please post it, even if it doesn't work.

    If it doesn't work, please describe exactly what happens when you run it. "No luck" covers many things, including doing nothing, crashing, or causing the penguin on top of the television to explode. We need to know which of those you mean by "no luck" or "doesn't work".
     
  4. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #4
    Jesus what compiler are you using?!
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    distcc with the Python(Monty) extension.
     
  6. Abrexas thread starter macrumors member

    Joined:
    Jul 29, 2011
    #6
    Sorry that my post was vague. The code I gave you was from a test of mine, so all the variables are different then what I have, so I will start over.

    I called my text field "Label" because I got it by dragging 'Label' to my window in the Interface Builder. Because of this it is of type id.

    Code:
    IBOutlet id myDisplay;
    Under my button click when I run

    Code:
    [myDisplay setStringValue: [NSString stringWithFormat: @"%s", array[randElement]]];
    It does ALMOST what I want it to. Every time I click the button, a new randomly generated element of the array shows up in the label.

    When I try using stringByAppendingFormat and other append syntaxes I get the same error which is
    " 'NSString' may not respond to +stringByAppendingFormat "

    I am going to assume that my problem is using an NSString and not an NSMutableString as balamw suggested. I will let you know if it works, however I get the feeling you already know weather or not it will haha.

    Thank you for the feedback!!
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    Post the actual code that causes that message. We need to see exactly the code you're using, exactly as you wrote it.

    Then reread the NSString class reference doc, and look for a +stringByAppendingFormat method. I predict you won't find one. That's because stringByAppendingFormat is an instance method, not a class method. That means you need to send the message to an NSString instance, not the NSString class. If you don't know the difference, you should review the fundamentals.
     
  8. Abrexas, Aug 8, 2011
    Last edited by a moderator: Aug 8, 2011

    Abrexas thread starter macrumors member

    Joined:
    Jul 29, 2011
    #8
    Tried and failed.
    This is probably a stupid problem that I'm having, but I'm hella new to programming, and brand new to Objective-C.

    I changes NSString to NSMutableString and got the same error (just changes 'NSString' may not respond.... to 'NSMutableString' may not...)

    I'm just wondering, could it be a problem that my array is a C array? ie:
    char *array[] = {"...", "...", "..."};
    I tried doing it with an NSArray before and had the same problem, so I assumed that wasn't it, but I could easily be wrong. Other then that I don't see that I am using any other strings, so I don't know what to change to Mutable.

    This is how I used it
    Code:
    [myDisplay setStringValue: [NSString stringWithAppendingFormat: @"%s", array[randElement]]];
    I guess I don't really know the difference. If there a suggested page/document I should use to learn from?

    Regardless, could I still get some help making this particular code work?

    Thanks ^_^
     
  9. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #9
    Question 1: What is the difference between a class method and an instance method? (class methods are the ones starting with "+" and instance methods are the ones starting with "-").

    Question 2: How do you invoke a class method, and how do you invoke an instance method?

    Question 3: What kind of method is "stringByAppendingFormat" and how should it therefore be callled? And how do you call it?

    That should fix your problem.
     
  10. chown33, Aug 8, 2011
    Last edited: Aug 8, 2011

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #10
    What are you using to learn? A book? An online tutorial? Be specific. Title & author, or URL.

    If you're not using a learning resource (instructional book or tutorial), are you using a reference resource (language reference doc?)? If so, post exactly what it is.

    Do you have any other experience with any programming language? Again, be specific.


    Here are two reference docs (i.e. not tutorials):
    Object-Oriented Programming with Objective-C
    The Objective-C Programming Language

    Here's another reference, which specifically mentions class methods:
    http://en.wikipedia.org/wiki/Objective-C#Interface

    EDIT
    Here's another reference, covering fundamentals:
    Cocoa Core Competencies
     
  11. Abrexas, Aug 8, 2011
    Last edited by a moderator: Aug 8, 2011

    Abrexas thread starter macrumors member

    Joined:
    Jul 29, 2011
    #11
    Your solution to my problem was to tell me the questions that I just learned to ask. The third question was already answered and telling me what class and instance methods start with isn't helpful at all. I actually already knew that part. Doesn't tell me at all what the difference is.

    Your post fixes nothing, you fixed nothing. Please don't post if you aren't going to supply anything useful or insightful. Thanks

    So far the bulk of my learning comes from YouTube tutorials by macandcomuterhelp
    http://www.youtube.com/watch?v=ORyDm7J71Co
    I caught up to his most recent video (lesson 32) which is about switch statements. I don't have any books on Objective-C but I do have "Programming in C" by Stephen G. Kochan.

    Other then that I have taken two java (ise 108 and 208) coursed (one was only processing so I don't think that really counts) and an intro to C course (cse 130 i think). In both of those we didn't get much further then structs.

    Thank you for the references, I will look into those ASAP!
     
  12. kevinpascal, Aug 12, 2011
    Last edited: Aug 12, 2011

    kevinpascal macrumors newbie

    Joined:
    Aug 12, 2011
    #12
    Thanks for those links. I am also Objective C starter and your links are very helpful.:D
     
  13. romulus23 macrumors newbie

    Joined:
    Aug 14, 2011
    #13
    Note: I'm an absolute obj-c beginner myself, but have enough professional java programming exp. to answer your question.

    What does your code:

    Code:
    [myDisplay setStringValue: [NSString stringWithAppendingFormat: @"%s", array[randElement]]];
    It calls the setter of your button with a string argument to set a specific string. This specific string is your randomly selected string. Therefore the text on your button is always the randomly selected string.

    What can you do to set it to "old string + new randomly selected string"? You have two choices:

    1. call the getter of the text field on your button (returns a string) and send that string the message "stringByAppendingFormat" with the argument of the new randomly selected string. Thats the difference between instance methods and class methods, instance methods can only be called on instanced objects, like the string you get from the getter.

    2. call the setter with the string you get from a call to the getter + the new randomly selected string.

    As I'm a beginner in obj-c I can't give you the code as I'm quite sure the syntax would be not correct, but you should know where to go from now.

    romulus23
     

Share This Page