Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

TalShani

macrumors member
Original poster
Jun 29, 2010
45
0
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.
 
Yes, I read it few times, the problem is I don't know how and which one can help me.

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?
 
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?

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?
 
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?

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.
 
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
 
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.
 
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.

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.
 
it fails..

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?
 
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?
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.
 
Well, I can't really remember what's the ERROR is, because I'm on my iPhone.
Well, then, just be patient and wait until you are back at your computer. Then supply us with the error. It really will help.

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.
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?
 
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.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.