Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > Mac OS X Development Forums > Mac OS X Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-10-2010, 05:39 AM   #1 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 97
Question Source code linebreak in NSString constant

I hope it's not an obvious/dump question but I just don't know how to search for it in this forum or the web.

In my implementation file I have a rather long NSString constant. To improve readability I would like to break this constant into several lines.

So I am not looking for a linebreak in the displayed string, rather a way to break a string over several lines in the source code.

Here is an example in Java code. Is there something like a '+'-Operator for NSString as well?
Code:
String s = "this"
    + " is a"
    + " very long"
    + " string";
Thanks!
SteveMobs is offline   Reply With Quote
Old 02-10-2010, 05:45 AM   #2 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 97
Default

Oh, I'm sorry. Think I just found a solution myself by expermenting.

This seems to work:
Code:
NSString *s = @"this" \
    @" is a" \
    @" very long" \
    @" string!";
Can someone please confirm that this is the best way to do a linebreak?
SteveMobs is offline   Reply With Quote
Old 02-10-2010, 12:39 PM   #3 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

These are all valid ways to split a string across multiple lines:

Code:
	NSString *s = @"this" \
		@" is a" \
		@" very long" \
		@" string!";
	
	NSLog(s);
	
	
	NSString *s1 = @"this" 
		@" is a" 
		@" very long" 
		@" string!";
	
	NSLog(s1);
	
	NSString *s2 = @"this"
		" is a"
		" very long"
		" string!";
	
	NSLog(s2);
	
	NSString *s3 = @"this\
 is a\
 very long\
 string!";
	
	NSLog(s3);
The last one is probably the worst; it's hardest to understand what's in the string and what isn't, and it will break your indenting.

I prefer this one:
Code:
	NSString *s2 = @"this"
		" is a"
		" very long"
		" string!";
It's short and sweet. They all do the same thing, though.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-11-2010, 05:42 AM   #4 (permalink)
Registered Member
 
Join Date: Dec 2009
Posts: 97
Default

Thanks a lot!

.. I just wonder where to find out about things like that, is it covered by any documentation?
SteveMobs is offline   Reply With Quote
Old 02-11-2010, 12:44 PM   #5 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Quote:
Originally Posted by SteveMobs View Post
Thanks a lot!

.. I just wonder where to find out about things like that, is it covered by any documentation?
Sort of. The one you found is mentioned here:

Mac Dev Center: The Objective-C Programming Language: Language Summary

The one I like is a side effect of C strings; if you put a bunch of C string constants in a row with only whitespace separating them you get a single C string, which gets processed by the @ directive.

The backslash stuff comes from C, which lets you continue any line by ending it with a backslash:

"A continued line is a line which ends with a backslash, \. The backslash is removed and the following line is joined with the current one. No space is inserted, so you may split a line anywhere, even in the middle of a word. (It is generally more readable to split lines only at white space.)"

Unfortunately there's a lot of C stuff that isn't covered in iPhone or Objective-C books, so it pays to keep a copy of K&R handy.

Lurkers should note that *none* of these methods work if you're not dealing with constants. Every one of them works at compile time, not while the program is running.
__________________

Free Games!

Last edited by smasher; 02-11-2010 at 12:46 PM.
smasher is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On



» Advertisements
» Online Users: 234
16 members and 218 guests
ADY, Alsahir, cacao, dacapo, Dani77, Desert Diva, djohnson, F_Bryant, HemiMG, jansan, M@realobjects, MarkC, prchn4christ, smethorst, spiderguy84
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,882
Threads: 89,228
Posts: 380,762
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jansan
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:54 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0