在for ... where子句中测试enum是否相等

时间:2017-05-21 16:42:32

标签: swift for-loop enums

<?php /* * Template Name: */ get_header(); get_template_part('inc/carousel'); $the_query = new WP_Query( [ 'posts_per_page' => 14, 'paged' => get_query_var('paged', 1) ] ); if ($the_query->have_posts()) { ?> <div id="ajax"> <?php $i = 0; $j = 0; while ($the_query->have_posts()) { $the_query->the_post(); if ($i % 7 === 0) { // Large post: on the first iteration and every 7th post after... ?> <div class="row"> <article <?php post_class('col-sm-12 col-md-12'); ?>> <div class="large-front-container"> <?php the_post_thumbnail('full', array( 'class' => 'large-front-thumbnail' )); ?> </div> <h2><a class="front-page-post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p> <div class="front-page-post-info"> <a class="moretext" href="<?php the_permalink(); ?>">Read more</a> <?php get_template_part('front-shop-the-post'); ?> <?php get_template_part('share-buttons'); ?> <div class="front-comments"><?php comments_popup_link('0', '1', '%', 'comment-count', 'none'); ?></div> </div> </article> </div> <?php } else { // Small posts ?> <?php if ($j % 3 === 0) echo '<div class="row">'; ?> <article <?php post_class('col-md-4'); ?>> <?php the_post_thumbnail('full', array( 'class' => 'medium-front-thumbnail' )); ?> <h2><a class="front-page-post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p> <div class="front-page-post-info"> <a class="moretext" href="<?php the_permalink(); ?>">Read more</a> <?php get_template_part('front-shop-the-post'); ?> <?php get_template_part('share-buttons'); ?> <div class="front-comments"><?php comments_popup_link('0', '1', '%', 'comment-count', 'none'); ?></div> </div> </article> <?php $j++; if ($j % 3 === 0) echo '</div>'; ?> <?php } $i++; } ?> </div> <?php if (get_query_var('paged') < $the_query->max_num_pages) { load_more_button(); } } elseif (!get_query_var('paged') || get_query_var('paged') == '1') { echo '<p>Sorry, no posts matched your criteria.</p>'; } wp_reset_postdata(); get_footer(); 具有关联值时,如何测试枚举相等性?一个人为的例子:

case

(我知道我可以测试enum Status : Equatable { case success case failed(error: String) static func == (lhs: Status, rhs: Status) -> Bool { switch (lhs, rhs) { case (.success, .success), (.failed, .failed): return true default: return false } } } let statuses = [ Status.success, .failed(error: "error 1"), .failed(error: "error 2"), .success ] // Failed: Binary operator '==' cannot be applied to operands of type 'Status' and '_' for s in statuses where s == .failed { print(s) } ,但实际的枚举有更多的案例,所以它们很麻烦)

1 个答案:

答案 0 :(得分:4)

您可以使用if case

for status in statuses {
    if case .failed = status {
        ...
    }
}

但遗憾的是,您无法将casewhere循环的for子句一起使用。

但是,在这种情况下,因为您已将.failed定义为与另一个相等,而不管error相关值是什么,理论上您可以这样做:

for status in statuses where status == .failed(error: "") {
    show("\(status)")
}

我并不为这种模式而疯狂,因为(a)它取决于.failed值相等的事实,即使它们具有不同的error个关联值; (b)导致代码容易被误解。