3

I called method in another class (they are both singletons).

WebserviceHelper.h

@interface WebserviceHelper : NSObject { int currentType; NSString *SERVER_URL; WebserviceManager *webService; } @property (nonatomic, assign) id delegate; - (void)retrieveStudentwithCode:(NSString *)code { currentType = STUDENT_TYPE; NSString *param = [NSString stringWithFormat:@"token=uencom&cid=%@", code]; NSString *link = [NSString stringWithFormat:@"%@getStudentInfo", SERVER_URL]; [webService retrieveData:link withParameters:param]; } 

After call webservice and get data it cached here in received data. I check and it works fine but when it deliver to didFinishLoading error happen here

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)receivedData { data = [NSMutableData new]; [data appendData:receivedData]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { [self stopPostingToWebservice]; //it carsh here [delegate: data]; } 

Call stack:

2014-08-20 10:39:05.187 School-Link[1030:60b] -[WebserviceHelper :]: unrecognized selector sent to instance 0xa88a420 2014-08-20 10:39:05.188 School-Link[1030:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[WebserviceHelper :]: unrecognized selector sent to instance 0xa88a420' *** First throw call stack: ( 0 CoreFoundation 0x01bf31e4 __exceptionPreprocess + 180 1 libobjc.A.dylib 0x018f08e5 objc_exception_throw + 44 2 CoreFoundation 0x01c90243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275 3 CoreFoundation 0x01be350b ___forwarding___ + 1019 4 CoreFoundation 0x01be30ee _CF_forwarding_prep_0 + 14 5 School-Link 0x00030c82 -[WebserviceManager connectionDidFinishLoading:] + 242 6 Foundation 0x016b9e49 ___NSURLConnectionDidFinishLoading_block_invoke + 40 7 Foundation 0x016507e1 __65-[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]_block_invoke + 62 8 Foundation 0x014d8f5e -[NSURLConnectionInternalConnection invokeForDelegate:] + 119 9 Foundation 0x014d8ec6 -[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:] + 208 10 Foundation 0x014d8dd8 -[NSURLConnectionInternal _withActiveConnectionAndDelegate:] + 76 11 Foundation 0x014d9188 _NSURLConnectionDidFinishLoading + 43 12 CFNetwork 0x02a3169f ___ZN27URLConnectionClient_Classic26_delegate_didFinishLoadingEU13block_pointerFvvE_block_invoke + 111 13 CFNetwork 0x02a2f3de ___ZN27URLConnectionClient_Classic18_withDelegateAsyncEPKcU13block_pointerFvP16_CFURLConnectionPK33CFURLConnectionClientCurrent_VMaxE_block_invoke_2 + 104 14 CoreFoundation 0x01b94c69 CFArrayApplyFunction + 57 15 CFNetwork 0x02998441 _ZN19RunloopBlockContext7performEv + 155 16 CFNetwork 0x02a7a3f4 _ZThn16_N19RunloopBlockContext24multiplexerClientPerformEv + 20 17 CFNetwork 0x02998257 _ZN17MultiplexerSource7performEv + 299 18 CFNetwork 0x0299806c _ZN17MultiplexerSource8_performEPv + 76 19 CoreFoundation 0x01b7c77f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15 20 CoreFoundation 0x01b7c1d5 __CFRunLoopDoSources0 + 437 21 CoreFoundation 0x01b991ae __CFRunLoopRun + 910 22 CoreFoundation 0x01b989d3 CFRunLoopRunSpecific + 467 23 CoreFoundation 0x01b987eb CFRunLoopRunInMode + 123 24 GraphicsServices 0x0338d5ee GSEventRunModal + 192 25 GraphicsServices 0x0338d42b GSEventRun + 104 26 UIKit 0x005b0f9b UIApplicationMain + 1225 27 School-Link 0x00018f6d main + 141 28 libdyld.dylib 0x03026701 start + 1 29 ??? 0x00000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException 
2
  • The way your passing the object to your delegate is wrong! Follow the @trojanfoe syntax Commented Aug 20, 2014 at 9:00
  • i am new to objective c , so i dont have any experience in that Commented Aug 20, 2014 at 9:02

3 Answers 3

3

Try this, have a protocol declaration in WebserviceHelper.h like

@protocol studentDataDelegate <NSObject> -(void)WebserviceHelper:(WebserviceHelper *)webserviceHelper didStudentDataDownloadCompleteWithData:(NSMutableData *)data; @end 

WebserviceHelper.m

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { [self stopPostingToWebservice]; [self.delegate WebserviceHelper:self didStudentDataDownloadCompleteWithData:data]; } 

so which ever class confirms to the above protocol should implement the delegate method like,

@interface RequestingDataClass : UIViewController <studentDataDelegate> 

by doing this you will receive the warning that you have not implemented didStudentDataDownloadCompleteWithData:method so do it like

 -(void)WebserviceHelper:(WebserviceHelper *)webserviceHelper didStudentDataDownloadCompleteWithData:(NSMutableData *)data; { webserviceHelper.delegate=self; // Do something with the `data` } 
Sign up to request clarification or add additional context in comments.

Comments

2

You do no show the declaration of the delegate, however this statement:

[delegate: data]; 

Should be:

[delegate haveSomeData:data]; 

(or something similar)

2 Comments

should i create new method in anther class to handle the data from this one
@MohammadShaker Well if you want to inform the delegate that data has been received, which is what I assume you'd like to do, you will need a method similar to that shown in my answer.
0

did you create protocol in your WebserviceHelper.h file like this?

@protocol WebserviceHelperDelegate <NSObject> @required -(void)reciveData:(NSData *)data;//or your method @end 

decleare a property

@property (nonatomic,strong) id <WebserviceHelperDelegate> delegate; 

then call this method like

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection { [self stopPostingToWebservice]; //it carsh here [self.delegate reciveData: data]; } 

And implement this method from where you call webservice class and assign delegate

-(void)reciveData:(NSData *)data{ } 

1 Comment

i try this code , it make many problem with me , i work in my friend code , i dont know how he build it , but thank you friend

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.