Reputation: 250
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
Upvotes: 1
Views: 1393
Reputation: 71008
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?
Upvotes: 2