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

  • SEARCH
  • Home
  • 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 7771121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:34:16+00:00 2026-06-01T16:34:16+00:00

I’ve ran into some issues using ^blocks in Objective-C. I’m trying to set an

  • 0

I’ve ran into some issues using ^blocks in Objective-C. I’m trying to set an instance variable from within a block – I’ve read through some Apple documentation on the topic, and I feel I have tried everything.

@interface MyClass
{
    // I have tried all possible combinations using __weak, __strong and __block.
    __weak __block NSMutableArray *filenames;
}

// *.m
static ASIFormDataRequest *g_request = nil;

@implementation MyClass
-(void) funnymethod
{
    filenames = [NSMutableArray array];
    [filenames addObject:@"This is a string."];
    NSLog(@"%@", filenames);

    g_request = [InitializerClass initializeRequest];
    [g_request setCompletionBlock:^
    {
        filenames = [NSMutableArray array];
        [filenames addObject:@"This is another string."];
        NSLog(@"%@", filenames);
    }];

    [g_object startASynchronous];
}
@end

The code above gives the following output:
( “This is a string.” )
(null)

That sucks. So, I’ve tried different combinations of __weak, __strong and __block – and anything else gives the following output:
( “This is a string.” )
( “This is another string.” )
BUT! There’s a massive but. The completion block is never exited. The activity indicator in the top bar indicating an open connection keeps spinning, and the screen becomes unresponsive.

How can I set the filenames-object from within the block successfully? Thanks in advance.

  • 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-06-01T16:34:17+00:00Added an answer on June 1, 2026 at 4:34 pm

    What the qualifiers do:

    __block this qualifier allows a closure to modify the value stored in the given variable.

    __weak is a reference to an object that does not prevent the object from being de-allocated.

    __strong is a reference to an object that does prevent the object from being de-allocated.

    What you need to do:

    __weak doesn’t do what you want because it doesn’t prevent your array from being de-allocated after the current scope ends. Since you are making an async call, there is nothing preventing the run-time from reclaiming the memory used by the array before your block is executed.

    __strong will retain the object past the end of the current scope. This is what you want.

    __block will allow your block to modify the specified variable, however this isn’t needed when referencing an instance variable since self will be automatically retained.

    In a reference-counted environment, by default when you reference an
    Objective-C object within a block, it is retained. This is true even
    if you simply reference an instance variable of the object. Object
    variables marked with the __block storage type modifier, however, are
    not retained.

    Note: In a garbage-collected environment, if you apply both __weak and
    __block modifiers to a variable, then the block will not ensure that it is kept alive. If you use a block within the implementation of a
    method, the rules for memory management of object instance variables
    are more subtle:

    If you access an instance variable by reference, self is retained;

    I think your problem lies here (relevant portions in bold):

    You can specify that an imported variable be mutable—that is, read-write— by applying the __block storage type modifier. __block storage is similar to, but mutually exclusive of, the register, auto, and static storage types for local variables.

    __block variables live in storage that is shared between the lexical scope of the variable and all blocks and block copies declared or
    created within the variable’s lexical scope. Thus, the storage will
    survive the destruction of the stack frame if any copies of the blocks
    declared within the frame survive beyond the end of the frame (for
    example, by being enqueued somewhere for later execution). Multiple
    blocks in a given lexical scope can simultaneously use a shared
    variable.

    As an optimization, block storage starts out on the stack—just like
    blocks themselves do. If the block is copied using Block_copy (or in
    Objective-C when the block is sent a copy), variables are copied to
    the heap. Thus, the address of a __block variable can change over
    time.

    There are two further restrictions on __block variables: they cannot
    be variable length arrays
    , and cannot be structures that contain C99
    variable-length arrays.

    Instead of an NSMutableArray, try using a plain NSArray using

    + (id)arrayWithObject:(id)anObject.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.