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 > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-07-2009, 05:31 AM   #1 (permalink)
New Member
 
Join Date: May 2009
Posts: 3
Question Declaring a string variable

Hi,

I'm just starting to learn Objective-C.........very simple question for you...

How do I declare a string variable?

At the moment I am using:

Code:
NSString *colour;
But when I go to printf the contents of this variable, nothing is displayed.

I'm writing a small program to try and understand the syntax in objective-c, so please forgive my immaturity in the code below:-

Code:
#import <Cocoa/Cocoa.h>
#import <stdio.h>


@interface Cup : NSObject
{
	BOOL empty1;
	NSString *colour;
}

-(void)drinkCup:(BOOL)done;
-(void)paintCup:(id)paint;
-(void)print;

@end

@implementation Cup

-(void)drinkCup:(BOOL)done

{

	empty1 = done;
	
}

-(void)paintCup:(id)paint
{

	colour = paint;
	
}

-(void)print

{

	printf("The cup has been drunk:%i\n", empty1 );
	printf("The cup colour is:%@", colour );
	
	
}



@end


int main(int argc, char *argv[])
{
	Cup *lukesCup = [[Cup alloc] init];
	
	[lukesCup drinkCup: 1];
	[lukesCup paintCup: @"Red"];
	[lukesCup print];
	
	[lukesCup release];
	
    return NSApplicationMain(argc,  (const char **) argv);
	
}
Do I need to init the string as it is based on the class NSString?

Thanks for your help.
carbcycle is offline   Reply With Quote
Old 05-07-2009, 05:42 AM   #2 (permalink)
_pi
New Member
 
Join Date: Apr 2009
Posts: 24
Default

use NSLog to output strings

NSString has a class method called stringWithString that takes care of alloc, init and handling the release of the newly allocated string object.

As a rule of thumb: class methods that return a "ready-to-go" object also take care of memory management, so no need to call release on an object created through a class method.


Hope this helps.
__________________
easyFind - the web search tool for your iPhone - type less, search faster
_pi is offline   Reply With Quote
Old 05-07-2009, 05:47 AM   #3 (permalink)
New Member
 
Join Date: May 2009
Posts: 3
Default

Quote:
Originally Posted by _pi View Post
use NSLog to output strings

NSString has a class method called stringWithString that takes care of alloc, init and handling the release of the newly allocated string object.

As a rule of thumb: class methods that return a "ready-to-go" object also take care of memory management, so no need to call release on an object created through a class method.


Hope this helps.
Thank for the quick reply.

I understand what you are saying about "ready-to-go" object but not sure how I would use NSLog in my code. Can you demonstrate?

Cheers.
carbcycle is offline   Reply With Quote
Old 05-07-2009, 07:09 AM   #4 (permalink)
_pi
New Member
 
Join Date: Apr 2009
Posts: 24
Default

Quote:
Originally Posted by carbcycle View Post
Thank for the quick reply.

I understand what you are saying about "ready-to-go" object but not sure how I would use NSLog in my code. Can you demonstrate?

Cheers.
Sure. It's important to keep in mind, that NSLog simply takes 1 NSString object, so you either need to concatenate multiple strings together using the method "stringByAppendingString" or you can create one yourself using [NSString stringWithFormat] which allows to use placeholders (such as %d, %f, %@, and so on)

Code:
NSString *s = [NSString stringWithString:@"this is the content of the string"];	

NSLog([[NSString stringWithString:@"the string contains: "] stringByAppendingString:s]);

NSLog([NSString stringWithFormat:@"the string contains: %@", s]);

Hope this helps.
__________________
easyFind - the web search tool for your iPhone - type less, search faster
_pi is offline   Reply With Quote
Old 05-07-2009, 08:29 AM   #5 (permalink)
New Member
 
Join Date: May 2009
Posts: 3
Default

Quote:
Originally Posted by _pi View Post
Sure. It's important to keep in mind, that NSLog simply takes 1 NSString object, so you either need to concatenate multiple strings together using the method "stringByAppendingString" or you can create one yourself using [NSString stringWithFormat] which allows to use placeholders (such as %d, %f, %@, and so on)

Code:
NSString *s = [NSString stringWithString:@"this is the content of the string"];	

NSLog([[NSString stringWithString:@"the string contains: "] stringByAppendingString:s]);

NSLog([NSString stringWithFormat:@"the string contains: %@", s]);

Hope this helps.
Thats great thank you.
carbcycle is offline   Reply With Quote
Old 05-07-2009, 12:11 PM   #6 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,178
Default

FYI, you can use printf() to display NSString strings. You just have to make the proper conversion, as follows:
Code:
NSString * myString = @"This is my string."

printf( "myString is '%s'\n", [myString UTF8String] );
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Reply

Bookmarks

Tags
nsstring, string, variable

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
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,765
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

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