Results 1 to 4 of 4
  1. #1
    Which of these two ways are the best NSString Initializer ?
    Bracer's Avatar
    Member Since
    Oct 07, 2009
    Location
    Singapore
    Posts
    14
    Which of these two ways are the best NSString Initializer ?
    What is the "correct"way of declaring NSString ?

    Code:
    -(void) PrintMessage
    {
    	NSString* Variable1 = [[NSString alloc] initWithCString: "This is Variable 1 String." ];
    	NSString* Variable2 = @"This is Variable 2 String.";
    	
    	NSLog(@"\n\n%@\n%@",Variable1,Variable2 );
    }
    I also realize this works:
    Code:
    id Variable1 = [[NSString alloc] initWithCString: "This is Variable 1 String." ];
    id Variable2 = @"This is Variable 2 String.";
    	
    NSLog(@"\n\n%@\n%@\n\n",Variable1,Variable2 );
    printf("\nThis is \"printf\" in action: \n%s\n%s\n",[Variable1 cString],[Variable2 cString]);
    "id" is so amazing ! It's like the "Object" or "[]" in Actionscript 3 or the Variant type in Visual Basic !
    I am very surprise that Variable2 just "knows" that it's a NSString, I figure it's smart enough to know that when the value assigns to it is of NSString type and it convert itself automatically, absolute brilliant from my point of view ;p

  2. #2
    Which of these two ways are the best NSString Initializer ?

    Member Since
    May 02, 2009
    Posts
    480
    Specs:
    MBP 2.33 4GB: MacPro 8 Core 2.8, 16GB: MacMini 2.26 4GB: MacMin 2.53 4GB: iPhone3GS 32GB
    NSString* Variable2 = @"This is Variable 2 String.";
    This would be considered the 'correct' form, though both forms accomplish the same thing. The first approach is simply too verbose and the initializer provides nothing beyond what the direct initialization accomplishes.

    I am very surprise that Variable2 just "knows" that it's a NSString, I figure it's smart enough to know that when the value assigns to it is of NSString type and it convert itself automatically
    Actually, the language itself helps here. The '@' token essentially allows the compiler to do the necessary conversion from the character string to the NSString.

  3. #3
    Which of these two ways are the best NSString Initializer ?
    Bracer's Avatar
    Member Since
    Oct 07, 2009
    Location
    Singapore
    Posts
    14
    Thank you

  4. #4
    Which of these two ways are the best NSString Initializer ?
    xstep's Avatar
    Member Since
    Jun 25, 2005
    Location
    On the road
    Posts
    3,231
    Specs:
    2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
    CameraTime - Time lapse photography for novice and advanced users.

    When asking questions, post the version of your software. You'll receive better answers.

    Please post your results to the thread as it is good feedback.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 06-02-2010, 12:43 PM
  2. NSString containing IvP4 of machine
    By chriseccles in forum OS X - Development and Darwin
    Replies: 1
    Last Post: 04-25-2010, 06:18 AM
  3. Ways to make my iBook better
    By jmack549 in forum Apple Notebooks
    Replies: 0
    Last Post: 06-29-2007, 10:23 PM
  4. New ways
    By Going2fast in forum Switcher Hangout
    Replies: 4
    Last Post: 04-16-2006, 08:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •