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 8816595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:39:23+00:00 2026-06-14T04:39:23+00:00

#import ViewController.h @implementation ViewController int tnt=1; -(void)myMethod{ void(^blk)(void) = ^(void){ tnt = 3+1; NSLog(@tnt=>%d,

  • 0
#import "ViewController.h"

@implementation ViewController

int tnt=1;

-(void)myMethod{

    void(^blk)(void) = ^(void){
        tnt = 3+1;
        NSLog(@"tnt=>%d", tnt);
    };

    blk();
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self myMethod];
}

The variable tnt is outside but inside block its working perfectly fine. If i declare the tnt variable inside myMethod it start giving the __block required error.

In both case the tnt variable is outside of the block. Apple docs also says variable outside need __block storage type. Why its working when tnt is declared outside of myMethod.

  • 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-14T04:39:25+00:00Added an answer on June 14, 2026 at 4:39 am

    I would guess that is related to scope. From the documentation:

    Variables local to the enclosing lexical scope declared with the
    __block storage modifier are provided by reference and so are mutable. Any changes are reflected in the enclosing lexical scope, including
    any other blocks defined within the same enclosing lexical scope.
    These are discussed in more detail in “The __block Storage Type.”

    When you put int tnt=1; outside the lexical scope, I think you won’t have to use the __block modifier. And you have an example, here. You can also read the following:

    __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.

    Since the int tnt=1; is declared outside the lexical scope, it will survive the destruction of the stack.

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

Sidebar

Related Questions

import org.joda.time.LocalDate; public class Test { public static void main(String[] args) { long time=System.currentTimeMillis();
import java.util.Scanner; public class CourseSplitter { public static void main(String args[]){ Scanner keyboard =
Here is my code: Header: #import <UIKit/UIKit.h> @interface ViewController : UIViewController { UIImageView *imageView;
EDIT 1: changed the code to: delegate.h: #import <UIKit/UIKit.h> @class ViewController; @interface AppDelegate :
import java.awt.*; import javax.swing.*; import javax.swing.GroupLayout; /** * @author Ene Ion */ public class
import MySQLdb import sys from libdesklets.controls import Control from IDBConnection import IDBConnection class DBConnection(Control,
import httplib2 from urllib import urlencode h = httplib2.Http() h.add_credentials('zackster@gmail.com', 'PassWord') data = dict(key=ThisIsMyApiKeyICopiedAndPastedIt)
import java.io.*; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class ExplicitChannelRead { /** * @param args
import re , strings , os ,sys sentence = abcdefghijkl for i in range(0,len(sentence),3):
import xlrd book = xlrd.open_workbook(File_1.xls) sheet = book.sheet_by_index(0) print sheet.row_values(0)[0] I am trying to

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.