我正在尝试使用 NSPredicate 测试某些特定 Assets 集合是否仅包含一种媒体类型/子类型。 testForPhotosPredicate 工作得很好,但是当尝试使用 testForPanoramasPredicate 时失败并显示消息:无法解析格式字符串“mediaSubtypes & %i”
如何在此谓词中为 mediaSubtypes 使用位掩码?
for (PHFetchResult *newFetch in collectionFetches)
{
for (PHAssetCollection *sub in newFetch)
{
PHFetchResult *assetsInCollection = [PHAsset fetchAssetsInAssetCollection:sub options:fetchOptions];
NSArray *allAssets = [assetsInCollection objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, assetsInCollection.count)]];
if (allAssets.count > 0)
{
[allAssetsArray addObjectsFromArray:allAssets];
NSPredicate *testForPhotosPredicate = [NSPredicate predicateWithFormat:@"mediaType = %i",PHAssetMediaTypeImage];
NSArray *testForAllPhotos = [allAssets filteredArrayUsingPredicate:testForPhotosPredicate];
if (testForAllPhotos.count == allAssets.count)
{
NSPredicate *testForPanoramasPredicate = [NSPredicate predicateWithFormat:@"mediaSubtypes & %i",PHAssetMediaSubtypePhotoPanorama];
NSArray *testForAllPanoramas = [testForAllPhotos filteredArrayUsingPredicate:testForPanoramasPredicate];
if (testForAllPanoramas.count == testForAllPhotos.count)
{
NSLog(@"all panos");
}
}
}
}
}
最佳答案
我相信我已经用下面的代码解决了这个问题:
NSPredicate *testForPanoramasPredicate = [NSPredicate predicateWithFormat:@"(mediaSubtypes & %i) == %i",PHAssetMediaSubtypePhotoPanorama,PHAssetMediaSubtypePhotoPanorama];
NSArray *testForAllPanoramas = [testForAllPhotos filteredArrayUsingPredicate:testForPanoramasPredicate];
如果您不想执行初始图像谓词,可能会更好:
NSPredicate *testForPanoramasPredicate = [NSPredicate predicateWithFormat:
@"(mediaType == %i && (mediaSubtypes & %i) == %i))",
PHAssetMediaTypeImage,PHAssetMediaSubtypePhotoPanorama,PHAssetMediaSubtypePhotoPanorama];
帮助其他特别想检查专辑是否包含特定媒体子类型的人的完整代码;用于在相册上显示 UI 角标(Badge)。
PHFetchResult *assetsInCollection = [PHAsset fetchAssetsInAssetCollection:sub options:fetchOptions];
if (assetsInCollection.count > 0)
{
NSArray *allAssets = [assetsInCollection objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, assetsInCollection.count)]];
NSPredicate *testForPanoramasPredicate = [NSPredicate predicateWithFormat:@"(mediaType == %i && (mediaSubtypes & %i) == %i))",PHAssetMediaTypeImage,PHAssetMediaSubtypePhotoPanorama,PHAssetMediaSubtypePhotoPanorama];
NSPredicate *testForHDRPredicate = [NSPredicate predicateWithFormat:@"(mediaType == %i && (mediaSubtypes & %i) == %i))",PHAssetMediaTypeImage,PHAssetMediaSubtypePhotoHDR,PHAssetMediaSubtypePhotoHDR];
NSPredicate *testForVideosPredicate = [NSPredicate predicateWithFormat:@"mediaType = %i",PHAssetMediaTypeVideo];
NSPredicate *testForSlomoPredicate = [NSPredicate predicateWithFormat:@"(mediaType == %i && (mediaSubtypes & %i) == %i))",PHAssetMediaTypeVideo,PHAssetMediaSubtypeVideoHighFrameRate,PHAssetMediaSubtypeVideoHighFrameRate];
NSPredicate *testForTimelapsePredicate = [NSPredicate predicateWithFormat:@"(mediaType == %i && (mediaSubtypes & %i) == %i))",PHAssetMediaTypeVideo,PHAssetMediaSubtypeVideoTimelapse,PHAssetMediaSubtypeVideoTimelapse];
NSPredicate *testForStreamedPredicate = [NSPredicate predicateWithFormat:@"(mediaType == %i && (mediaSubtypes & %i) == %i))",PHAssetMediaTypeVideo,PHAssetMediaSubtypeVideoStreamed,PHAssetMediaSubtypeVideoStreamed];
NSArray *testForAllPanoramas = [allAssets filteredArrayUsingPredicate:testForPanoramasPredicate];
NSArray *testForAllHDR = [allAssets filteredArrayUsingPredicate:testForHDRPredicate];
NSArray *testForAllVideos = [allAssets filteredArrayUsingPredicate:testForVideosPredicate];
NSArray *testForAllSlomo = [allAssets filteredArrayUsingPredicate:testForSlomoPredicate];
NSArray *testForAllTimelapse = [allAssets filteredArrayUsingPredicate:testForTimelapsePredicate];
NSArray *testForAllStreamed = [allAssets filteredArrayUsingPredicate:testForStreamedPredicate];
NSArray *allPossibilitiesArray = @[testForAllPanoramas,testForAllHDR,testForAllVideos,testForAllSlomo,testForAllTimelapse,testForAllStreamed];
for (NSArray *sub in allPossibilitiesArray)
{
if (sub.count == allAssets.count)
{
PHAsset *firstAsset = sub.firstObject;
[dataSource setObject:[NSNumber numberWithInt:firstAsset.mediaSubtypes] forKey:@"MediaSubtype"];
}
}
}
关于ios - 带位掩码的 NSPredicate 用于过滤 NSArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30789546/
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我已经在Sinatra上创建了应用程序,它代表了一个简单的API。我想在生产和开发上进行部署。我想在部署时选择,是开发还是生产,一些方法的逻辑应该改变,这取决于部署类型。是否有任何想法,如何完成以及解决此问题的一些示例。例子:我有代码get'/api/test'doreturn"Itisdev"end但是在部署到生产环境之后我想在运行/api/test之后看到ItisPROD如何实现? 最佳答案 根据SinatraDocumentation:EnvironmentscanbesetthroughtheRACK_ENVenvironm
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
当我使用has_one时,它工作得很好,但在has_many上却不行。在这里您可以看到object_id不同,因为它运行了另一个SQL来再次获取它。ruby-1.9.2-p290:001>e=Employee.create(name:'rafael',active:false)ruby-1.9.2-p290:002>b=Badge.create(number:1,employee:e)ruby-1.9.2-p290:003>a=Address.create(street:"123MarketSt",city:"SanDiego",employee:e)ruby-1.9.2-p290
是否有简单的方法来更改默认ISO格式(yyyy-mm-dd)的ActiveAdmin日期过滤器显示格式? 最佳答案 您可以像这样为日期选择器提供额外的选项,而不是覆盖js:=f.input:my_date,as::datepicker,datepicker_options:{dateFormat:"mm/dd/yy"} 关于ruby-on-rails-事件管理员日期过滤器日期格式自定义,我们在StackOverflow上找到一个类似的问题: https://s
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
我有一个名为Post的类,我需要能够适应以下场景:如果用户选择了一个类别,则只显示该类别的帖子如果用户选择了一种类型,则只显示该类型的帖子如果用户选择了一个类别和类型,则只显示该类别中该类型的帖子如果用户没有选择任何内容,则显示所有帖子我想知道我的Controller是否不可避免地会因大量条件语句而显得粗糙...这是我解决此问题的错误方法-有谁知道我如何才能做到这一点?classPostsController 最佳答案 您最好遵循“胖模型,瘦Controller”的惯例,这意味着您应该将这种逻辑放在模型本身中。Post类应该能够报告
我正在我的Rails项目中安装Grape以构建RESTfulAPI。现在一些端点的操作需要身份验证,而另一些则不需要身份验证。例如,我有users端点,看起来像这样:moduleBackendmoduleV1classUsers现在如您所见,除了password/forget之外的所有操作都需要用户登录/验证。创建一个新的端点也没有意义,比如passwords并且只是删除password/forget从逻辑上讲,这个端点应该与用户资源。问题是Grapebefore过滤器没有像except,only这样的选项,我可以在其中说对某些操作应用过滤器。您通常如何干净利落地处理这种情况?
我正在使用带有Rails的Devise,我想添加一个方法“getAllComments”,所以我这样写:classUser在我的Controller中:defdashboard@user=current_user@comments=@user.getAllComments();end当我访问我的url时,我得到了undefinedmethod`getAllComments'for#我做错了什么?谢谢 最佳答案 因为getAllComments是一个类方法,而您正试图将其作为实例方法访问。您要么需要访问它:User.getAllCom