Objective C NSString vs Java String objects

Discussion in 'Mac Programming' started by txhockey9404, Dec 30, 2011.

  1. txhockey9404 macrumors 6502a

    Feb 25, 2008
    I'm pretty new to Objective C, but I know quite a bit about Java and a bit about C++. What I'm trying to do is combine two strings and put a "to" in the middle.

    In my Java program, I have two Strings, "startdate" and "enddate". I am combining them into a third String, "str" using the following line of code:
    str = startdate + "to" + enddate;

    I'm trying to do the exact thing in Objective-C, but I am having quite a bit of trouble with the NSString class. I have used the same variable names and wrote the following line of code:
    str = @"%@ to %@", startdate, enddate;

    I have a feeling that is totally wrong. I also wrote the following line of code, thinking it may be better:
    str = *startdate + @" to " + *enddate;

    What should I be doing?
    Thanks for the help!
  2. Sydde macrumors 68020


    Aug 17, 2009
    It is wrong, but not totally. NSStrings only respond to method calls, not operators. You should learn up a bit on what pointers are and how to use them, since that is how you reference NSStrings.

    What you need to do is put you argument into a method call, such as -stringWithFormat: – if you do not understand what that means, you need to learn Objective-C basics.
  3. Mark FX macrumors regular

    Mark FX

    Nov 18, 2011
    West Sussex, UK
    The equivelent in Objective-C to your Java code would be this.

            NSString *startDate = @"01-01-2012";
            NSString *endDate = @"01-01-2013";
            NSString *combinedDate = [NSString stringWithFormat:@"%@ to %@", startDate, endDate];
            NSLog(@"%@", combinedDate);
    But this assumes that your dates are Strings and not NSDate's.

    Like Syyde say's do some reading up on basic Objective-C.

    Regards Mark
  4. txhockey9404 thread starter macrumors 6502a

    Feb 25, 2008
    Thank you guys very much! Solved my problem completely! I actually have a brand new book on Objective-C coming in the mail tomorrow. Just wanted to experiment a little bit before it came.
  5. PatrickCocoa macrumors 6502a

    Dec 2, 2008
    Cocoa is your daddy

    One way to think of Objective C (and Cocoa) is that the nice people at Apple have already thought of everything. So instead of looking through your mental collection of 20 or so primitive operations (like +, -, sqrt, etc.), look though the five or ten thousand Cocoa methods.
  6. Sydde macrumors 68020


    Aug 17, 2009
    But by comparison, once you have learned a few score of factory methods, you can almost write code without looking at the docs, because the methods are so verbose, they just about document themselves. The real trick is learning how the pieces fit together.
  7. PatrickCocoa macrumors 6502a

    Dec 2, 2008
    I agree with you, and with me

    Absolutely. By "look through the five or ten thousand Cocoa methods" I include using autocomplete. Just type "[NSString ", hit the escape key, and see what autocomplete pops up for you.

    Or type
    NSString *myWonderfulStringOfExcitement;
    hit escape, and get all of the instance methods.

    Once you know NSString and that it has a bunch of interesting methods, you can use the documentation, or as you suggest, use autocomplete to give you the method names and pick the one that looks like it will work.

    My original point remains, that the "memorize-twenty-primitives-and-build-my-own-methods" should be replaced in Objective-C/Cocoa by "where-has-Steve-hidden-the-method-that-will-do-what-I-want?".
  8. liavman macrumors 6502

    Sep 22, 2009
    Ha..ha... true.

    In one sense, Objective-C shows its age. It is from late 1980s. Other languages provide a lot more syntactic sugar for common operations like '+' etc whereas with Objective-C's pure OO ideology you have to send messages to classes and instances. The messaging passing paradigm is absolutely brilliant but with common operations it becomes a chore.

    Like how they introduced the '.' notation for properties which is a departure from the message passing syntax, may be they can somehow incorporate more natural operators for numbers and strings. Newcomers to Objective-C definitely get turned off at how unnatural the basic string handling is. Most modern languages elevate strings to first class objects and rightfully so.
  9. txhockey9404 thread starter macrumors 6502a

    Feb 25, 2008
    Thanks a lot for the help again guys! Objective-C is a really really cool language. I'm used to the dot operator, so it's taking some getting used to, but wow Apple put a lot of work into it. The book I got is Programming in Objective-C Fourth Edition by Stephen Kochan. Amazing book, very helpful. I've already gotten through 1/3 of it. (Couldn't do anything yesterday, too out of it)
  10. Sydde macrumors 68020


    Aug 17, 2009
    Cocoa actually elevates strings beyond "first class objects". The syntactic sugar you suggest becomes somewhat murky and problematic in some situations.

    For example, if I use -stringByAppendingPathComponent: I know that the right number of path separators will be used (no extra slash if one is already there) and, more importantly, the result will be a properly escaped PathStore2 object ready for use by the file manager (I believe both halves of the string will be reformatted if need be). Then, if the string is a path, and it contains something like a "茶" character in it, and I ask for the string length, it will resolve that as one character for me, not the 12 or 16 bytes it actually occupies as escaped.

    So, what would a "+" operator really mean? The verbose syntax of Objective-C and Cocoa forces you to be clear on what kind of concatenation you want to do while at the same time abstracting away some of the housekeeping. If nothing else, it can relieve you a bit of the work of commenting your code. And if you have to look up a method, hopefully you will read the doc on it and not be surprise by some subtle behavior that might confound you later.
  11. liavman macrumors 6502

    Sep 22, 2009
    I understand what you are saying. I do not know exactly what will work, but my first thought is along the lines of the '.' semantics. It does not try to solve all the issues but a convenient short hand for getters and setters. A related issue is synthesize. They do the synthesis of the getters and setters for you but there are a lot of cases that is not good enough, in those cases you have to write your own getters and setters. Along those lines, '+' will take care of the basic concatenation. If you want more, you have to go to the fully spelled out method names.

    [myInstance name:mad:"firstname" + @"lastname"]

    if something like this works, people can get by with strings easily since a lot of programming with strings are at a such a basic level and it is so clean looking.
  12. Sydde macrumors 68020


    Aug 17, 2009
    You may think that is clean looking, I disagree.

    Most programs that do stuff use variables rather than constants, so this example is not really very useful. When you see "+" between two variables, your first instinct is "addition", not "concatenation", because that is what "+" means. It is a primitive operation, for primitive types, which is not what NSStrings are, or even C strings for that matter. The representation of a NSString is a pointer, which you could do primitive addition on, though that would usually not be a good idea.

    The advantage to the Objective-C/Cocoa syntax is that it sacrifices some moments of your time in the name of clarity, which I think is worth the price. The effort involved in learning a method name and typing it out where you might have used a single character is, IMO, returned in the form of more readable, unambiguous source.

Share This Page