NSString print tag name

Discussion in 'iOS Programming' started by ipadzero, Oct 23, 2011.

  1. ipadzero macrumors newbie

    Joined:
    Oct 23, 2011
    #1
    Hi, I want to display the tag name of my CCSprite

    For example
    CCSprite* abc = ............;
    abc.tag = Sprite1Tag;

    Is there any way to use NSString to display the name "Sprite1Tag"

    Thanks a lot
     
  2. Matthew Yohe macrumors 68020

    Joined:
    Oct 12, 2006
    #2
    Uhm, could you provide more of an explanation of what you want?

    The tag on a CSSprite is an integer, and so... it's not very clear what you really want here.

    If you just want to print the tag, you can log it: NSLog(@"tag: %d",abc.tag);
     
  3. ipadzero thread starter macrumors newbie

    Joined:
    Oct 23, 2011
    #3
    Thanks for your quick reply,
    I want to use a label control to show the tag name of my sprite in the top left corner when I touch it , your code is only show the tag number, what I want to show here is "Sprite1Tag"
     
  4. Matthew Yohe macrumors 68020

    Joined:
    Oct 12, 2006
    #4
    Oh, so then just NSLog(@"Sprite%dTag",abc.tag);
     
  5. ipadzero thread starter macrumors newbie

    Joined:
    Oct 23, 2011
    #5
    "Sprite1Tag" is just an example, my tag name can be anything, won't follow that format
     
  6. Matthew Yohe macrumors 68020

    Joined:
    Oct 12, 2006
    #6
    Uhm, then you haven't really told me what you want.

    Please be very explicit, you're not being so.

    Give me real examples of what you want to occur.

    ----------

    And also, remember your tag can only be a number, not a string. So, it can't be "anything"
     
  7. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #7
    In this code, Spring1Tag is not an NSString. It's an identifier and it doesn't exist after your code is compiled. So there's nothing you can directly do to print that identifier when your program runs.

    However there are other techniques for doing this. One technique is to maintain an array of the sprite name (as NSString objects) and then set the sprite's tag to be the index of the name in that array.
     
  8. ipadzero thread starter macrumors newbie

    Joined:
    Oct 23, 2011
    #8
    My example is in my 2nd post. Sorry if I didn't explain it clearly

    Hmm, so I guess there's no other way can do that directly.
    Thanks all
     
  9. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #9
    The problem with your post was that you took a well known problem of the C, C++, Objective-C, Java and other languages, and hid it away in a context with language that made it very hard to find out what you wanted. When you ask for help, remember that nobody can read your mind, so you have to pose your question in a very clear way. (BTW. Quite often that alone makes it unnecessary to ask).

    I tend to write a class method "stringForTag:(NSInteger)tag" which converts enum values to strings with a switch statement.
     

Share This Page