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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 04-13-2010, 03:03 PM   #1 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 8
bate5150 is on a distinguished road
Default NSString / NSMutableString issue

Hi, this is a total newbie question and I've already resigned myself to the fact that I'm making some fundamental error.

Anyway, I'm making a simple app just to acclimate myself with Obj-C and iPhone. It has buttons for each of the 10 numbers. When the buttons are touched up they invoke the numPressed method. This method reads the tag of the button and puts it into an NSString (myS). I try to append the NSString to the NSMutableString newString, which is then outputted to a UILabel.

The problem I'm running into is that newString doesn't appear to have anything in it when it is displayed. If I use myS instead of newString it is displayed properly. The desired behavior is for the label to display the characters the user inputs following one another.



implementation:
Code:
#import "TestAppDelegate.h"

@implementation TestAppDelegate

@synthesize window;
@synthesize hey;
@synthesize newString;

- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    	
	[window makeKeyAndVisible];
	[[newString initWithCapacity:40]alloc];
	
	return YES;
}


- (void)dealloc {
    [window release];
    [super dealloc];
}

- (IBAction) numPressed: (id) sender
{
	NSString *myS = [NSString stringWithFormat:@"%i", [sender tag]];
	[newString appendString:myS];

//     This also doesn't change anything
//     [newString myString:myS];

//     "hey" is the UILabel
	hey.text = newString;
}
@end
advice would be welcomed, thanks
bate5150 is offline   Reply With Quote
Old 04-13-2010, 03:07 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

To start with:
Code:
[[newString initWithCapacity:40]alloc];
should be
Code:
newString = [[NSMutableString alloc] initWithCapacity:40]
smithdale87 is offline   Reply With Quote
Old 04-13-2010, 03:08 PM   #3 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Code:
[[newString initWithCapacity:40]alloc];
When creating a new object, alloc will be first, then init. And the alloc method will pretty much always be sent to a class, not a variable.

Also, this doesn't work. You are sending a message to newString, but newString doesn't exist yet.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 04-13-2010, 03:57 PM   #4 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 8
bate5150 is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
Code:
[[newString initWithCapacity:40]alloc];
When creating a new object, alloc will be first, then init. And the alloc method will pretty much always be sent to a class, not a variable.

Also, this doesn't work. You are sending a message to newString, but newString doesn't exist yet.
Thank you both for the very quick responses. That was a little embarrassing but like I said, I figured there was at least one fundamental error on my part.

Anyway, I was still having difficulty getting the NSMutableString to behave like I desired so this is what I did instead:

Code:
- (IBAction) numPressed: (id) sender
{
	//myS is an instance variable now, newNum is the most recently
        //pressed button, newNum is added to myS and sent to the label

	NSString *newNum = [NSString stringWithFormat:@"%i", [sender tag]];
	
	if (myS == nil)
		myS = newNum;
	
	else
		myS = [myS stringByAppendingString:newNum];
	
	hey.text = myS;
	
}
This does what I was trying to do (simply display numbers the user enters... lol). It doesn't use the NSMutableString class at all, but I don't like that I wrote this because I couldn't get mutable objects to work for me...
bate5150 is offline   Reply With Quote
Old 04-13-2010, 08:10 PM   #5 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by bate5150 View Post
Thank you both for the very quick responses. That was a little embarrassing but like I said, I figured there was at least one fundamental error on my part.

Anyway, I was still having difficulty getting the NSMutableString to behave like I desired so this is what I did instead:

Code:
- (IBAction) numPressed: (id) sender
{
	//myS is an instance variable now, newNum is the most recently
        //pressed button, newNum is added to myS and sent to the label

	NSString *newNum = [NSString stringWithFormat:@"%i", [sender tag]];
	
	if (myS == nil)
		myS = newNum;
	
	else
		myS = [myS stringByAppendingString:newNum];
	
	hey.text = myS;
	
}
This does what I was trying to do (simply display numbers the user enters... lol). It doesn't use the NSMutableString class at all, but I don't like that I wrote this because I couldn't get mutable objects to work for me...

Why not post the mutable string code that isn't working?

Once you fix your [[NSMutableString alloc] initWithCapacity: x] problem, the rest of the code looked like it would work.

Show us the way you defined your properties too. The way you are assigning those properties needs some cleanup. You need to learn good memory management habits.

Regards,

Duncan C
WareTo
Duncan C is offline   Reply With Quote
Old 04-13-2010, 08:28 PM   #6 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 60
imsatasia is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
Why not post the mutable string code that isn't working?

Once you fix your [[NSMutableString alloc] initWithCapacity: x] problem, the rest of the code looked like it would work.

Show us the way you defined your properties too. The way you are assigning those properties needs some cleanup. You need to learn good memory management habits.

Regards,

Duncan C
WareTo
Try The following code:

Code:
NSMutableString *myS = [[NSMutableString alloc] init];;
[myS setString:@""];
then

Code:
NSString *newNum = [NSString stringWithFormat:@"%i", [sender tag]];
	
	if (myS == nil)
		[myS appendString:newNum];
	
	else
		[myS appendString:newNum];
	
	hey.text = myS;
__________________
--
mySite
imsatasia is offline   Reply With Quote
Old 04-14-2010, 01:53 PM   #7 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 8
bate5150 is on a distinguished road
Default

Quote:
Originally Posted by imsatasia View Post
Try The following code:

Code:
NSMutableString *myS = [[NSMutableString alloc] init];;
[myS setString:@""];
then

Code:
NSString *newNum = [NSString stringWithFormat:@"%i", [sender tag]];
	
	if (myS == nil)
		[myS appendString:newNum];
	
	else
		[myS appendString:newNum];
	
	hey.text = myS;
This worked, thank you
bate5150 is offline   Reply With Quote
Reply

Bookmarks

Tags
nsmutablestring, nsstring, uilabel

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: 314
15 members and 299 guests
chemistry, Domele, Fstuff, givensur, heshiming, HowEver, iAppDeveloper, iphonedevshani, jbro, JoeRCruso, kapps11, newDev, SLIC, stanny, WheyLabs
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,648
Threads: 94,112
Posts: 402,874
Top Poster: BrianSlick (7,990)
Welcome to our newest member, brandon6031
Powered by vBadvanced CMPS v3.1.0

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