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

grapes911

Moderator emeritus
Original poster
Jul 28, 2003
6,995
10
Citizens Bank Park
I'm forced to do a project in Objective-C (I usually use C/C++ or Java), so this may seem like a dumb or basic question.

I have two NSStrings and a char:

// This is the string containing the file's text
// path was defined earlier and this seems to work
myString = [[NSString alloc] initWithContentofFile:path];

// This is the string I want to create
NSString *tempString = [[NSString alloc] initWithString:mad:""];

// temp char to put 1st char of myString into
char *tempChar;

I want to take the first character of myString and put it into tempChar. I'm going to test tempChar and then either discard it or put it to the end of tempString.

So to make it easy, can someone give me some lines of code that:
1. Set tempChar to the first char of myString
2. Remove the first letter of myString but keep the rest of the string
3. Add the char in tempChar to the end of tempString

Thanks for any help
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Problem 1: NSStrings are immutable (i.e. cannot be changed). Use NSMutableString for the string you are going to add to.

Problem 2: Cocoa is Unicode based (this is a good thing). Do not use char, use unichar (this allows for 2-byte characters).

You can get a character (unicode) with [myString characterAtIndex:index]; where index is an int, zero based.

You can append to the mutable string with [myMutableString appendFormat:mad:"%C",char]; where char is a unichar.

If you only want to use 8-bit characters and normal C you can convert to and from standard C strings (i.e \0 terminated char[]) using the methods in NSString.

So to answer your questions:
NSString *myString = [[NSString alloc] initWithContentsOfFile:path];
NSMutableString *tempString = [[NSMutableString alloc] initWithCapacity:[myString length]];
int i;
unichar c;
for (i=0;i<[myString length];i++)
{
c = [myString characterAtIndex:i];
if (<TEST c here...>)
{
[tempString appendFormat:mad:"%C",c];
}
}

Seems to compile for me (with Path replaced with @"" and the fake test replaced with 1).

Edit to add: note that myString is not altered. We do not remove anything from it (as it's immutable, the only way to remove the first character would be to create a new object which can be costly).
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
grapes911 said:
Thanks. I'm going to try it later tonight.

PS. Why did I know you were going to be the one to respond? ;)

No problem. I'm actually glad to see people trying to get to grips with Cocoa. It's not that different in many ways to Java (i.e. strings are immutable in Java too, it just hides it better). I'm normally a Java developer too (well, Java/Perl/Shell/Sybase/...) but I honestly think Cocoa and Objective-C are better for GUI desktop apps.
 

grapes911

Moderator emeritus
Original poster
Jul 28, 2003
6,995
10
Citizens Bank Park
Another Question.
My comparisons aren't working. I need to compare the unichar with a NSString. Is this possible? Or should I do this another way.

c is a unichar
s is a nsstring

if ([c isEqualTo:s]) {
...
}
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
grapes911 said:
Another Question.
My comparisons aren't working. I need to compare the unichar with a NSString. Is this possible? Or should I do this another way.

c is a unichar
s is a nsstring

if ([c isEqualTo:s]) {
...
}

A character cannot equal a string! Is your NSString a single character? If so use the method above to remove the first (and only) character from the string and compare them that way. Otherwise tell me what you want to do (for example are you trying to see if the character is in the string, in a range of characters, for example a white-space character or what).

Edit to add: you have made a kind of basic error here. c is not an object. It's a basic type (like char, int and so on). So you can't pass it messages like a subclass of NSObject.
 

Mr. Durden

macrumors 6502a
Jan 13, 2005
716
0
Colorado
Well, I've never made of chair out of string, but I think if you wrapped the string tight enough and with good knots... Hey, wait a second. You spelled chair wrong... Oh. Oh, I see. Carry on then.
 

grapes911

Moderator emeritus
Original poster
Jul 28, 2003
6,995
10
Citizens Bank Park
robbieduncan said:
A character cannot equal a string! Is your NSString a single character? If so use the method above to remove the first (and only) character from the string and compare them that way. Otherwise tell me what you want to do (for example are you trying to see if the character is in the string, in a range of characters, for example a white-space character or what).

Edit to add: you have made a kind of basic error here. c is not an object. It's a basic type (like char, int and so on). So you can't pass it messages like a subclass of NSObject.


I've got it. I've found out about scanner. It made things much easier. Next time I have a problem, maybe I should just say what I need to do and someone will point me to the right methods. Thanks for the help.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
grapes911 said:
I've got it. I've found out about scanner. It made things much easier. Next time I have a problem, maybe I should just say what I need to do and someone will point me to the right methods. Thanks for the help.

Perhaps. I personally think that by playing around with the string methods you might have learnt a few things that will be usefull going forward.
 

grapes911

Moderator emeritus
Original poster
Jul 28, 2003
6,995
10
Citizens Bank Park
robbieduncan said:
Perhaps. I personally think that by playing around with the string methods you might have learnt a few things that will be usefull going forward.

Thats true. I did learn some things. I really like learning new languages, but for some reason I still perfer to code in C++. I hoping to get more into cocoa developement though.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.