New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Which of these two ways are the best NSString Initializer ?


Post Reply New Thread Subscribe

 
Thread Tools
Bracer

 
Bracer's Avatar
 
Member Since: Oct 07, 2009
Location: Singapore
Posts: 14
Bracer is on a distinguished road

Bracer is offline
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
QUOTE Thanks
wmprice1240

 
Member Since: May 03, 2009
Posts: 480
wmprice1240 has a spectacular aura about
Mac Specs: MBP 2.33 4GB: MacPro 8 Core 2.8, 16GB: MacMini 2.26 4GB: MacMin 2.53 4GB: iPhone3GS 32GB

wmprice1240 is offline
Quote:
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.

Quote:
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.
QUOTE Thanks
Bracer

 
Bracer's Avatar
 
Member Since: Oct 07, 2009
Location: Singapore
Posts: 14
Bracer is on a distinguished road

Bracer is offline
Thank you
QUOTE Thanks
xstep

 
xstep's Avatar
 
Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
xstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to all
Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid

xstep is offline
Bracer, you may want to review Introduction to The Objective-C 2.0 Programming Language

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.
QUOTE Thanks

Post Reply New Thread Subscribe


« what does the NS stand for? | Odd permission denied after compilation »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Airport Disk: Different Ways to Connect theonlydonfelix Internet, Networking, and Wireless 0 03-28-2009 12:23 PM
Ways of using Windows XP on my Mac Ricky55 Running Windows (or anything else) on your Mac 4 02-12-2008 11:03 AM
Ways to tell the difference between a Cocoa app and a Carbon app remain OS X - Apps and Games 0 05-22-2007 09:55 AM

All times are GMT -4. The time now is 11:18 PM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?