问题:
if (file.filePathUrl) {
url = file.filePathUrl;
}else{
url = [NSURL fileURLWithPath:file.filePath];
}
比如这个 如果 file中filePathUrl不存在就会闪退
问: 怎么会出现 对象中某个字段不存在呢?
定义了一个方法,方法中传一个ModelFile,实际上使用者穿的是 STShowFile
而 STShowFile中没有定义filePathUrl字段
这样使用上面的方法就会闪退
怎么判断对象中某个属性是否能用?
instancesrespondToSelector 和 respondsToSelector
instancesrespondToSelector 用法
[AVPlayerItem instancesRespondToSelector:@selector(duration)] ;
可以看到对象下所有的属性
respondsToSelector 用法
UIDevice* device = [UIDevice currentDevice];
if ([device respondsToSelector:@selector(isMultitaskingSupported)]) {
_multitaskingSupported = device.multitaskingSupported;
}
只能看到对象中的属性
这里只能用 respondsToSelector 方法 因为 file实际上是 STShowFile ,使用ModelFile判断是不对的,写法如下
if ([file respondsToSelector:@selector(filePathUrl)] && !NULLString(file.filePathUrl.absoluteString)) {
url = file.filePathUrl;
} else if([file respondsToSelector:@selector(filePath)] && !NULLString(file.filePath)) {
url = [NSURL fileURLWithPath:file.filePath];
} else {
return;
}
网友评论