1

I am a new iOS developer, i try to access instance variable in class method but i can't access.please give me some solution here is my code

h.file

@protocol ServiceProvideDelegate <NSObject>
@required
-(void)responeFromURL:(NSDictionary*)dicResponse;
@end

@interface AFNetworkServiceProvider : NSObject
{
   id <ServiceProvideDelegate> delegate;
}
@property (nonatomic,strong) id delegate;

+(NSDictionary*)getResponseFromURL:(NSURL*)url;
@end

m.file

@implementation AFNetworkServiceProvider
@synthesize delegate;
+(void)getResponseFromURL:(NSURL *)url
{
 @try
 {
    NSURLRequest *request=[NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *requestOperation=[[AFHTTPRequestOperation alloc]initWithRequest:request];
    requestOperation.responseSerializer=[AFJSONResponseSerializer serializer];

    [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
    {
        if(delegate)
        {
          [delegate responeFromURL:responseObject]
       } 
    }
    failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Response Error :- %@",[error localizedDescription] );
    }];
    [requestOperation start];    
}
@catch (NSException *exception)
{

}
@finally {

}
}

Try to Returne Response object using protocol method, but i get the error at if(delegate) unable to access instance variable in class method

6
  • 1
    So where in this code are you trying to access the instance variable? What code is it that is failing? Commented May 7, 2015 at 18:47
  • 2
    You can't access an instance variable in a class method - no instance exists. Commented May 7, 2015 at 19:17
  • 1
    Ah, I see your question (you've titled it completely wrong) - You can't return data from an asynchronous operation, you have to use a callback Commented May 7, 2015 at 19:20
  • 1
    @Hiren: You should ask your "return dictionary from block" as a separate question (@Adam addresses that above). Commented May 7, 2015 at 19:22
  • Thank Adam and Ben Zotton for your Response Commented May 8, 2015 at 16:05

1 Answer 1

2

You can't, and that's an intentional part of the design. For a given class, you can create as many instances of it as you want, but each is independent, and each has its own set of state (variables/properties).

A class method doesn't carry state, and doesn't know about any of the instances, so it can't know which instance's variables you want access to.

I assume you want access to the delegate. Perhaps your method should be an instance method instead here?

Sign up to request clarification or add additional context in comments.

1 Comment

Yes @ben I try to access delegate. Thank for your response.i understand from your answer that i have to use instances method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.