0

I have a class URLCache extends NSURLCache

in URLCache.m

+ (void)initialize {

    NSString *_cacheSubFolder = nil;
    NSUInteger _cleanCacheFilesInterval;
    if (_pageType == FirstPage) {
        _cleanCacheFilesInterval = FirstPageCleanCacheFilesInterval;
        _cacheSubFolder = @"/WebCatchedFiles/FirstPage/";
    }else if (_pageType == SecondPage){
        _cleanCacheFilesInterval = SecondPageCleanCacheFilesInterval;
        _cacheSubFolder = @"/WebCatchedFiles/SecondPage/";
    }else if (_pageType == ThirdPage){
        _cleanCacheFilesInterval = ThirdPageCleanCacheFilesInterval;
        _cacheSubFolder = @"/WebCatchedFiles/ThirdPage/";
    }

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    cacheDirectory = [[paths objectAtIndex:0] stringByAppendingString:_cacheSubFolder];
//    cacheDirectory = [cacheDirectory stringByAppendingString:@"/"];
    removeFilesInCacheInDueTime(cacheDirectory, _cleanCacheFilesInterval);
    createDirectry(cacheDirectory);
    supportSchemes = [NSSet setWithObjects:@"http", @"https", @"ftp", nil];
}

If A.m calls URLCache.m then A needs to send param _pageType into URLCache, I don't know how to send _pageType in. I tried

-(void)setPageType:(NSUInteger)pageType{
    _pageType = pageType;
}

but everytime in A.m

URLCache *sharedCache = (URLCache *)[NSURLCache sharedURLCache];
    [sharedCache setPageType:self.naviType];

got

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURLCache setPageType:]: unrecognized selector sent to instance 0xabd1470'

why cannot send param to NSURLCache?

How to send _pageType in?

Jason Zhao
  • 1,278
  • 4
  • 19
  • 36

1 Answers1

1
[NSURLCache sharedURLCache]

returns the shared URL cache instance. If you have not set a custom instance with setSharedURLCache:, this is an NSURLCache object.

The type cast (URLCache *) does not change the object and does not "convert" it to a URLCache object. You can verify that with

NSLog(@"class = %@", [sharedCache class]);

That is the reason why [sharedCache setPageType:...] throws an exception.

Note also that initialize is a special class method that is run once before any instances of that class are created (good explanation and links here: Objective-C: init vs initialize). It does therefore not make sense to check for _pageType in initialize.

So you have to create a instance of your URLCache class first, which you then can set as shared instance with

[NSURLCache setSharedURLCache:...]
Community
  • 1
  • 1
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382