NSString question...

  • If I have two lines of code:
    NSSLog("Test");
    NSLog(@"Test");
    I know that the first line is wrong, and the second line is correct.
    But I was wondering what the actual difference is?  I made the mistake
    of doing the first one, and got a sig11.

    Just wondering.
  • "Test" is just an array of characters.

    @"Test" is a NSString object.

    NSLog expects to have an NSString object as the first argument.  If
    you give it something else, it will try to read it as a NSString.
    Probably causing a crash or other funniness.

    SD
  • On Dec 16, 2003, at 2:58 PM, T Reaves wrote:

    > If I have two lines of code:
    > NSSLog("Test");

    this is a c string  (char* or variant)

    > NSLog(@"Test");

    this is a NSString* .

    > I know that the first line is wrong, and the second line is correct.
    > But I was wondering what the actual difference is?  I made the mistake
    > of doing the first one, and got a sig11.

    Since NSLog() looks for an NSString* argument, it tries to message your
    c string and since it is not an object it fails

    Chad

    >
    > Just wondering.
  • On Dec 16, 2003, at 1:58 PM, T Reaves wrote:

    > If I have two lines of code:
    > NSSLog("Test");
    > NSLog(@"Test");
    > I know that the first line is wrong, and the second line is correct.
    > But I was wondering what the actual difference is?  I made the mistake
    > of doing the first one, and got a sig11.
    >
    > Just wondering.

    Well @" ... " tells the compiler to create a NSString instance
    (actually a constant string subclass of NSString) while " ... " is just
    a plain old null terminated C string. I am sure this is covered in
    Apple's Objective-C docs, which can be found online.

    In the case of NSLog it expects an object not a null terminated string.

    -Shawn
  • This would work also (printf like syntax for printing scalars and
    CStrings):
    NSSLog(@"%s %d","Test",1);

    On Dec 16, 2003, at 2:23 PM, Shawn Erickson wrote:

    >> If I have two lines of code:
    >> NSSLog("Test");
    >> NSLog(@"Test");
    >> I know that the first line is wrong, and the second line is correct.
    >> But I was wondering what the actual difference is?  I made the
    >> mistake of doing the first one, and got a sig11.
    >>
    >> Just wondering.