25
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iOS 様々な比較パターンまとめ

Last updated at Posted at 2013-03-07

UIKit.framework, Foundation.frameworkなどiOSには様々な比較のメソッドや関数があります。
だいたい下記の3つのパターンに当てはまると思います。

1. 構造体 (CGRect, CGPoint, CGRange …)

Equal系の関数がある。

/* CGRect */
bool CGRectEqualToRect (
      CGRect rect1,
      CGRect rect2
);

/* CGPoint */
bool CGPointEqualToPoint (
	  CGPoint point1,
      CGPoint point2
);

2. クラス (NSString, UIColor …)

Equal系のメソッドがあったり、他のfameworkの関数を利用したり。

/* NSString */
- (BOOL)isEqualToString:(NSString *)aString


/* UIColor */
- bool CGColorEqualToColor (
		  CGColorRef color1,
		  CGColorRef color2
);
// 使用例
CGColorEqualToColor(color1.CGColor, color2.CGColor);

3. クラス (NSIndexPath, NSNumber …)

数値を扱ったり大小が比較出来るクラスの場合、NSComparisonResultを使用する比較メソッドがあります。(※ 更にoptionやrangeを指定出来るものもあります)

/* NSIndexPath */
- (NSComparisonResult)compare:(NSIndexPath *)indexPath
// 使用例    
NSComparisonResult result = [indexPath1 compare:indexPath2];
if (result == NSOrderedAscending) {
    NSLog(@"indexPath1はindexPath2より小さい");
} else if (result == NSOrderedDescending) {
    NSLog(@"indexPath1はindexPath2より大きい");
} else { // result == NSOrderedSame
    NSLog(@"indexPath1といndexPath2は等しい");
}
25
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?