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

MasterObiWan

macrumors newbie
Original poster
Jan 12, 2009
18
0
So even trying to do a simple thing like getting an index of a string is not easy in objective C.

with characterAtIndex I get handed the unicode of the string, so their must be a method to convert the unicode to string.

Were the people who created objective C kind enough to include a method to convert a unicode back to a string, or must I take out the unicode chart and do it all myself?

int hi = [@"hello world" characterAtIndex:1];
textView.text = [NSString stringWithFormat:mad:"%d", hi];
 

craig1410

macrumors 65816
Mar 22, 2007
1,129
905
Scotland
So even trying to do a simple thing like getting an index of a string is not easy in objective C.

with characterAtIndex I get handed the unicode of the string, so their must be a method to convert the unicode to string.

Were the people who created objective C kind enough to include a method to convert a unicode back to a string, or must I take out the unicode chart and do it all myself?

int hi = [@"hello world" characterAtIndex:1];
textView.text = [NSString stringWithFormat:mad:"%d", hi];

Hi,
Have a read of this article - it might help. By the way, I'm no expert I was just intrigued by your posting and did some googling. There may be better articles/explanations/guidance available from others.

http://rosscarter.com/2008/173.html?showcommentform=1

In short, it seems a bit messy... :(

Cheers,
Craig.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Code:
int hi = [@"hello world" characterAtIndex:1];
textView.text = [NSString stringWithFormat:@"%d", hi];

This looks pretty dangerous to me: characterAtIndex returns a unichar, not an int. Whilst this may well work, I'd say it's not a good idea...

And NSString does provide a way of turning an array of characters into a string: stringWithCharacters:length:. Just add you character to a unichar array with a single element and call this with length 1.
 

BorgCopyeditor

macrumors newbie
Jan 7, 2009
14
0
A character is not a string, hence the names of the methods. (But I agree that it's confusing at first.) If you really want to get part of a string, use the "substring..." methods.
Code:
NSString *helloFirstLetter = [@"Hello" substringWithRange:NSMakeRange(0,1)];
As far as I can tell, the "characters..." methods are there for when you need to work with textual data that is not already an NSString and/or cannot easily be converted to one by the factory methods. From the linked article:

For those who need to do their own low-level processing, and who are willing to handle Unicode complications themselves, we provide access to UTF-16 directly via characterAtIndex: et al., and to other representations with getBytes:… and related methods.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
So even trying to do a simple thing like getting an index of a string is not easy in objective C.

with characterAtIndex I get handed the unicode of the string, so their must be a method to convert the unicode to string.

Were the people who created objective C kind enough to include a method to convert a unicode back to a string, or must I take out the unicode chart and do it all myself?

int hi = [@"hello world" characterAtIndex:1];
textView.text = [NSString stringWithFormat:mad:"%d", hi];

What do you actually try to achieve?

There are lots of methods that manipulate the contents of a string, and they are usually there for some reason.
 

MasterObiWan

macrumors newbie
Original poster
Jan 12, 2009
18
0
These posts helped very much, I finally understand now why my program was crashing into pieces.

For some reason when I looked at the return value type "unichar" for characterAtIndex I thought it meant it would return unicode :rolleyes:.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.