Analyse a sentence.

Discussion in 'iOS Programming' started by TalShani, Jun 29, 2010.

  1. TalShani macrumors member

    Joined:
    Jun 29, 2010
    #1
    Hello everyone!
    So I'm new in this area of Objective C..
    I'm now working on a new iPhone application,
    While I worked on it I found a little problem ..
    I have a Text Field, Button and Label.
    I need to analyze the sentence that people will put in Text Field,
    Once they click the button, the Label will show the last word of the sentence.
    I just can not break the sentence,
    I know how to make string out of it, it's not a problem,
    My problem is to analyze it.. I think I need to use the command -length,
    but I don't really knows how..
    Hope you will reply soon,
    Tal Shani.

    NOTE: IT CAN BE ALSO A LONG WORD, its dont have to be sentence.
    I need to check if its a long word, if yes, it will copy it into other string,
    if its a sentence, I need it to copy only the last word to other string.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
  3. TalShani thread starter macrumors member

    Joined:
    Jun 29, 2010
    #3
    Yes, I read it few times, the problem is I don't know how and which one can help me.
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Are you serious? I linked to one, specific, method that would be helpful. If you don't even bother following the advice people link for you then why should any of us bother trying to help?
     
  5. TalShani thread starter macrumors member

    Joined:
    Jun 29, 2010
    #5
    Oh, sorry,
    i accidentally went to the documentation myself and didn't got the link..
    Can you give me an example for how I can use this method?
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    No. For the following reasons:

    1) Programmers should be able to read the documentation and implement code based on it. This is a core programming skill (regardless of language used) and should be learnt at the earliest possible stage
    2) In this case it's very simply. You want the last word in a sentence. This method will break a string up into components based. So you could use it to break a sentence into words and take the last word out of the array
    3) Finally: giving people sample code tends to have them copy&pasting without understanding anything about what they are copying leading to them not learning anything, asking the same sort of question over.

    If you really can't work it out I suggest Google.
     
  7. PizzaTray macrumors newbie

    Joined:
    Oct 31, 2009
    #7
    Example:
    Code:
    NSString *string      = txtField.text;
    NSArray  *arrWords = [string componentsSeparatedByString:@" "];
    
    if([arrWords count]!=1) lbl.text = [arrWords lastObject];          //lastWord
    else                     lbl.text = [arrWords objectAtIndex:0]; //longOneWord
    
     
  8. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #8
    Your question is too long and poorly specified. You need to ask something more along the lines of

    I have some text @"this is some text" and I want to parse that generate: whatever. How should I approach this problem?

    At any rate NSString has methods that can be used to chop it up into its parts. Also look at NSScanner for ways to chop up strings.
     
  9. TalShani thread starter macrumors member

    Joined:
    Jun 29, 2010
    #9
    OK, got it, but can you give me the place and example how to write the command (only)?
    When i wrote:
    Code:
    	NSString *list = @"hello i am tal";
    	NSArray *listItems = [list componentsSeparatedByString:@" "];
    it fails..
    hope you will help me,
    Tal.
     
  10. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #10
    You need to be more descriptive. What fails? You get a compile-time error? If so on which line and what is the error? If not then I assume it doesn't do what you expected? If this is the case what did you expect and, exactly, what did you get?
     
  11. TalShani thread starter macrumors member

    Joined:
    Jun 29, 2010
    #11
    Well, I can't really remember what's the ERROR is, because I'm on my iPhone.
    The one thing I do know is that I don't really know how to use this command, so I need a sample of the code that I can learn from it the template for this command.
    Hope you will answer me as soon as possible,
    Thanks,
    Tal Shani.
     
  12. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    Well, then, just be patient and wait until you are back at your computer. Then supply us with the error. It really will help.

    You've already been provided sample code in this very thread. You've even provided it yourself in this post. What else do you need?
     
  13. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #13
    It does not fail. Insert this into practically any working build-able project and see what it writes to the console
    Code:
    	NSString *list = @"hello i am tal";
    	NSArray *listItems = [list componentsSeparatedByString:@" "];
    	for (NSString *s in listItems){
    		NSLog(@"%@", s);
    	}
    
    What must be failing is either what you are doing with that array, or the fact that the returned array is autoreleased and will disappear on you if you don't retain it.
     

Share This Page