Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6800189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:56:21+00:00 2026-05-26T18:56:21+00:00

I just wrote a delegate method, that shall return an array, I’ve created inside

  • 0

I just wrote a delegate method, that shall return an array, I’ve created inside this method. Here is the code for that:

- (NSArray *) receivedString:(NSString *)rx{
NSString *portsetting = [[NSString alloc] init];
NSMutableArray *portArray = [[NSMutableArray alloc] initWithCapacity:[portsetting 
length]];
portsetting = rx;

for (int i = 0; i < [portsetting length]; i++)
{
    NSString *ichar = [NSString stringWithFormat:@"%c",[portsetting 
characterAtIndex:i]];
    [portArray addObject:ichar];
}

return portArray;
}

This delegate method is part of a library I’ve imported, an is not called by myself, but by the program searching for that method in my code. So far so good…. everything is working fine.

but here is the thing. I would like to get access to this returned array in my code, but I don’t want to call the method itself, because I don’t have any input values for that. This is all handled by the “program”… difficult to explain myself.

So after the method has been called, I would like to get access to this array, maybe by a method I would have written myself. But I don’t have a clue so far, how to do that…… Can someone give me a helping hand?

Is it necessary to init the array outside this function to get access to it? And how would I implement that? In the init-part of the code?
is there a way to make a copy of this array in another method?

Thank you
Sebastian

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:56:22+00:00Added an answer on May 26, 2026 at 6:56 pm

    In your class (the class that conforms to protocol and receives the callback), declare an instance variable and property for an NSMutableArray (or you could cast it as an array if it won’t change again after you first receive it).

    When your delegate method is called, assign your portArray to your class property before you return. Then your class instance has a reference to the portArray that can accessed by any method in your class. You don’t need to explicitly allocate/init an array for this, because you already have the array in memory by the time your callback method is finishing – assigning the array to your class property retains the it, thus asserting ownership beyond the scope of the delegate callback.

    You could call your own method from within the delegate callback (receivedString), and it would have access to your array.

    In your header (myClass.h):

    @interface myClass <ConformsToReceivedStringProtocol>
    {
    @private
        NSMutableArray *portArray_;
    }
    
    @property (nonatomic, retain) NSMutableArray *portArray;
    
    -(void) myMethodThatUsesPortArray;
    

    In your implementation (myClass.m)

    @implementation myClass
    
    @synthesize portArray = portArray_;
    
    -(NSArray *) receivedString:(NSString *)string
    {
    
    //do everything you're already doing
    
    NSMutableArray *myPortArray;
    
    [self setPortArray:myPortArray];
    
    [self myMethodThatUsesPortArray];
    
    return myPortArray;
    
    }
    
    -(void) myMethodThatUsesPortArray 
    {
    
    //do whatever you want with
    
    NSMutableArray *myArray = [self portArray];
    }
    
    //don't forget to release the property
    -(void) dealloc 
    {
    
    [self setPortArray:nil];
    [portArray_ release];
    
    [super dealloc];
    }
    

    A final option would be just to pass the array to your own method directly before you return, avoiding the need to define ivars and properties, but requiring you to manage the memory of the array yourself:

    - (void) myMethodThatUsesArray:(NSMutableArray *)portArray
    {
    [portArray retain];
    //do something with portArray and then release ownership
    [portArray release];
    }
    
    -(NSArray *) receivedText:(NSString *)string
    {
    //assume we have a myPortArray (balanced in memory)
    [self myMethodThatUsesArray:myPortArray]
    return myPortArray;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just wrote my first web service so lets make the assumption that my
I just wrote a new web part and now I am getting this error
I just wrote this into my WebForms .aspx: <span id=GenerateChartButton runat=server></span> I went to
I just wrote some code to test the behavior of std::equal, and came away
Here's an interesting problem that I have just come across. It is possible to
I'd like to make a delegate that invokes a specific instance method, unfortunately, it
I just wrote the following C++ function to programmatically determine how much RAM a
I just wrote an if statement in the lines of if (value == value1
I just wrote some new utility methods within a non-page class for an existing
I just wrote a PHP login script, and what I'm trying to accomplish is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.