Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Stunner

macrumors newbie
Original poster
Sep 14, 2009
22
0
I am having trouble with something relatively simple (or so I think so), but I can't figure out what I need to do to make it right. So here is the deal... I have a block of code that I use frequently throughout my application so I thought I should just make it into a function. So I did so and the function takes nothing and returns a (NSString *) and I try assigning the returned value to a temporary NSString * that the label will be assigned to, but I get this warning:
Code:
/!\ warning:'NSString' may not respond to '-makeMinsToGo' (Messages without a matching method signature will be assumed to return 'id' and accept '...' as arguments.)
So I think I know what I need to do, I need to have the function return the string itself rather than a pointer to the string, but I don't know how to do that. Google hasn't been much help. Here is my code:
Code:
//function:
- (NSString*) makeMinsToGo {
	/***...code...(showing last bit of function code)***/
	NSString *minsToGoLabelText = [[[NSString alloc] initWithString:
								   [timeToGoFormatter stringFromDate:dateComponentsTimeToGo]] autorelease];
	return minsToGoLabelText;
}

//makes function call in this code block
	NSString *temp = [[NSString alloc] initWithString:[temp makeMinsToGo]];
        //previously mentioned warning shows up right here
	minsToGo.text=  temp;
	//[minsToGo.text makeMinsToGo]; //this doesn't work either
	[temp release];

Thanks in advance for any help whatsoever. :)
 
I tried dereferencing the pointer (*returnVal) but the compiler didn't like that either. All I really need is an example of how to go about this and I'll be set. Any takers? Thanks.
 
The method you wrote looks good, but it only applies to the class you wrote it in; you need to call [self makeMinsToGo] or [instanceOfMyClassWithTheMethod makeMinsToGo] .

If you want to add a new method to "NSString" then you need to write a "category" that extends NSString - it's very easy though. Search for "Categories" on this page:

http://www.cocoadevcentral.com/d/learn_objectivec/
 
I tried dereferencing the pointer (*returnVal) but the compiler didn't like that either. All I really need is an example of how to go about this and I'll be set. Any takers? Thanks.
Based on your various threads, Stunner, I think it's time for you to step back from the real coding and go (re)learn the basics of Objective-C, OOP, etc. before you return to these problems. What is your education / experience with these things? Have you read any books, taken any classes, things of this nature?
 
Based on your various threads, Stunner, I think it's time for you to step back from the real coding and go (re)learn the basics of Objective-C, OOP, etc. before you return to these problems. What is your education / experience with these things? Have you read any books, taken any classes, things of this nature?

Perhaps I do need a refresher on C in general. I am a computer science major and I have taken some C and C++ courses in college and recently have been working extensively on making command line utilities with Python (which makes things so much easier to do, by the way). I would say I am at the intermediate programming level with Python at least. I understand the concepts and theory behind OOP and I try to practice good programming etiquette(such as creating functions with code that you use repeatedly, etc), but whenever I try doing that I end up getting stuck, and after 30minutes to an hour (or more) of googling on how to do what I want to do and getting nowhere I post my problem up on these forums(which are great by the way :) ).

I have followed "Beginning iPhone 3 Development Exploring the iPhone SDK" by Dave Mark and Jeff LaMarche and have been doing all of the examples it walks you through. The problem is that the book doesn't get very in-depth with many things. So yeah, I am just having problems with dealing with types mainly. I guess you could say Python has made me a bit "lazy".

Yeah so, I am sorry if I am annoying any of you out there with my constant noobie posts, but I am really trying to learn this stuff and am not asking you guys to write the code for me, just to push me in the right direction. If there is anything you think I should really have a look at feel free to let me know as I would like to improve. Thanks.
 
The method you wrote looks good, but it only applies to the class you wrote it in; you need to call [self makeMinsToGo] or [instanceOfMyClassWithTheMethod makeMinsToGo] .

If you want to add a new method to "NSString" then you need to write a "category" that extends NSString - it's very easy though. Search for "Categories" on this page:

http://www.cocoadevcentral.com/d/learn_objectivec/

Thanks for the tip xsmasher I'll check out the link.
 
Perhaps a read-through of a good Objective-C book will help get your mindset in the right place, since you seem to be weaker in the basics of Objective-C itself. I'll recommend "Programming in Objective-C 2.0" by Stephen Kochan.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.