我有一个类URLCache扩展NSURLCache
在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];
}如果A.m调用URLCache.m,那么A需要将param _pageType发送到URLCache中,我不知道如何将_pageType发送进来。我试过了
-(void)setPageType:(NSUInteger)pageType{
_pageType = pageType;
}但每次早上
URLCache *sharedCache = (URLCache *)[NSURLCache sharedURLCache];
[sharedCache setPageType:self.naviType];得到
由于“NSInvalidArgumentException”异常终止应用程序,原因:'-NSURLCache setPageType::未识别的选择器发送到实例0xabd1470‘
为什么不能发送param到NSURLCache?
如何将_pageType发送进来?
发布于 2012-11-13 18:07:20
[NSURLCache sharedURLCache]返回共享URL缓存实例。如果没有使用setSharedURLCache:设置自定义实例,则这是一个NSURLCache对象。
类型强制转换(URLCache *)不会更改对象,也不会将其“转换”为URLCache对象。你可以用
NSLog(@"class = %@", [sharedCache class]);这就是为什么[sharedCache setPageType:...]抛出异常的原因。
还请注意,initialize是一个特殊的类方法,在创建该类的任何实例之前只运行一次(这里有很好的解释和链接:Objective-C: init vs initialize)。因此,在_pageType中检查initialize是没有意义的。
因此,必须首先创建URLCache类的实例,然后可以将其设置为共享实例。
[NSURLCache setSharedURLCache:...]https://stackoverflow.com/questions/13364419
复制相似问题