String to Char to String

Moderator emeritus

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] initWithContentofFileath];

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

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

Moderator emeritus

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.

Moderator emeritus

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]) {
...
}

Click to expand...

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.

thread starterModerator emeritus

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.

Click to expand...

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.

Moderator emeritus

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.

Click to expand...

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.

MacRumors attracts a broad audience
of both consumers and professionals interested in
the latest technologies and products. We also boast an active community focused on
purchasing decisions and technical aspects of the iPhone, iPod, iPad, and Mac platforms.