I subclassed NSOperation and implemented the main method. When I added it to an NSOperationQueue, it was not being called. I compared it to sample code and sample projects that do work and could not see what the problem was. Does anyone have any experience with this problem and what causes it, please?
After going crazy trying to find the problem, I decided to start over from scratch, rebuild my class and test it at each step. In the header, I declared it a subclass of开发者_JAVA技巧 NSOperation. In the implementation file, I implemented main and only put a couple of lines: sleep(3)
; NSLog..;
. All good. Each step worked as I started to add properties, even declared delegate, etc.
Finally, in the main, I put code to use NSDirectoryEnumerator. Suddenly, main was not being called at all. Not only that, when I took that code back out, it still does not work!! I tried restarting XCode 4, restarting my machine (not that I know why these might help, but I'm running out of ideas) but all to no avail.
Any clues at all would be most appreciated. Thanks.
Update: I started over (again!) from scratch but this time I assembled the parts in a different order... and now it works! However, in case there is something that I'm doing wrong anyway and I've just been lucky this time round, here is the @interface section as @Andrew suggested:
@class ScanOperation;
@protocol ScanOperationDelegate
-(void)fileFound:(ScanOperation *)operation;
-(void)finishedScanOperation:(ScanOperation *)operation;
@end
#import <Foundation/Foundation.h>
@interface ScanOperation : NSOperation {
@private
NSObject <ScanOperationDelegate> *delegate;
NSURL *rootUrl;
int totCount, dirCount, filCount, errCount;
NSTimeInterval elapsed;
}
- (id)initWithScanRootUrl:(NSURL *)startAtUrl;
@property (readwrite, assign) NSObject <ScanOperationDelegate> *delegate;
@property (readonly) int totCount, dirCount, filCount, errCount;
@property (readonly) NSTimeInterval elapsed;
@end
I had this happen too. As usual it was me: I hadn't initialised the OperationQueue for the Operation to run on.
I met this question just now. 1) initialize the OperationQueue 2) add your operation to this queue 3) clean your project 4) run
精彩评论