Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Basic ObjC Question

Basic ObjC Question
Thread Tools
Forum Regular
Join Date: Sep 2000
Location: Ronneby, Sweden
Status: Offline
Reply With Quote
Nov 6, 2000, 07:49 PM
 
Hi, I'm probably just to tired to program but I can't get return to work. I have a class for storing pages of text (in a NSMutableArray) with the method getPage int)pageNumber I want that method to return a NSString. How do I do that?

- (NSString)getPage int)pageNumber

or should I have return type (NSString *) or (id)? And what should I write at the end of the method?

return [myArray objectAtIndex ageNumber -1];

or should I cast the returnvalue?

return (NSString *)[myArray objectAtIndex ageNumber -1];

Any help would be great. Maybe I'm just confused and mixing id's and pointers I don't know, must sleep...

/Gabbe
     
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Nov 6, 2000, 09:31 PM
 
I'm pretty new to objc myself, but try this.

- (NSString *)getPage: (int)pageNumber
{
return [[[myArray objectAtIndex: (pageNumber -1)] copy] autorelease];
}

This returns a pointer to a copy of the NSString stored in the NSArray myArray. If you wanted a pointer to the actual object stored in the array, leave of the copy and autorelease bit.

I didn't bother to try compiling that but the following is a similar piece of code I wrote last night that works fine.

m_hostname is of type NSString.

- (NSString *)getHostname
{
/*
Returns the hostname of the server associated with this instance.
ARGUMENTS:
NONE
RETURN:
A pointer to a copy of the hostname associated with this instance.
*/
return [[m_hostname copy] autorelease];
}

Terry
     
Dedicated MacNNer
Join Date: Nov 2000
Status: Offline
Reply With Quote
Nov 6, 2000, 09:33 PM
 
UBB did horrible things to my formatting. Sorry about that.
     
   
Thread Tools
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -5. The time now is 12:40 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2