Re: Obtaining the number of characters in a string
Re: Obtaining the number of characters in a string
- Subject: Re: Obtaining the number of characters in a string
- From: Ron Fleckner <email@hidden>
- Date: Sun, 20 Jul 2008 22:16:29 +1000
On 20/07/2008, at 9:56 PM, Phil Faber wrote:
Still struggling with documentation!
That might be because the documentation makes the reasonable
assumption that you already know how to use C and Objective-C.
Simple task. I have two text fields (text1 & text2) and all I'm
trying to achieve is to put into text2 a number specifying how many
characters there are in the text in text1.
I started with:
[text2 setStringValue:[text1 stringValue]];
..just to make sure I was on the right track and it successfully
placed a copy of the text1 text into text2. Now all I needed was
to change the code to put the character count instead.
I looked at the documentation and under NSString I found:
Getting a String’s Length
• – length
• – lengthOfBytesUsingEncoding:
• – maximumLengthOfBytesUsingEncoding:
So I click on 'length' and was told that this 'Returns the number
of Unicode characters in the receiver.' Not sure if that's the
same thing as just counting characters but changed my code to:
[text2 setStringValue:[text1 length]];
and got ...'warning: passing argument 1 of 'setStringValue:' makes
pointer from integer without a cast'
..so assuming this means I was trying to place a number into a text
field, I changed the code to:
[text2 setIntValue:[text1 length]];
It compiled and linked fine but the code didn't put anything into
text2!
Where am I going wrong?
You're not recognising the difference between the data types 'int'
and 'pointer to object'. The text field expects a pointer to an
NSString. Telling it to setIntValue:someInt is just you hoping it'll
work.
What you need to do is [text2 setStringValue:[NSString
stringWithFormat:@"%d", [text1 length]]];
You should read the documentation, paying attention to method return
types and etcetera. Also, bone up on basic C. It will help a lot.
Ron_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden