macOS/iOS API解説

iOS , Mac アプリケーション開発のために使われる主要フレームワークの日本語情報です。2010年代に書かれた内容です。今後更新はありません。

目次

compare:options:range:locale:

オプション付きで範囲を指定して文字列を比較して結果を返します
-(NSComparisonResult)compare:(NSString *)string
             options:(unsigned)mask
             range:(NSRange)compareRange
             locale:(NSDictionary *)dict

解説

オプション付きで範囲を指定して文字列を比較して結果を返します。
【比較オプション】
● NSCaseInsensitiveSearch 大文字小文字関係なく比較する
● NSLiteralSearch 大文字小文字を区別して比較する
レシーバの範囲を越えたらNSRangeExceptionを起こします。

返り値

( NSComparisonResult )

比較結果

引数

( NSString * )string

文字列

( unsigned )mask

比較オプション

( NSRange )compareRange

比較範囲

( NSDictionary * )dict

辞書

クラス

NSString

Instance Methods

使用可能

10.0

参照

例文

#import "MyObject.h"

@implementation MyObject

- (IBAction)myAction:(id)sender
{
NSString *str1 = [NSString stringWithString:@"a"];
NSString *str2 = [NSString stringWithString:@"b"];
NSString *str3 = [NSString stringWithString:@"c"];
NSDictionary *dic = 
[NSDictionary dictionaryWithObjectsAndKeys:
@"1",@"a",
@"1",@"b",
@"1",@"c",nil];

if ([str2 compare:str1 
            options:NSCaseInsensitiveSearch 
            range:NSMakeRange(0,3)
            locale:dic
            ] == NSOrderedSame){
            
            NSLog(@"YES");
        }else{
            NSLog(@"NO");
        }
if ([str3 compare:str1 options:NSCaseInsensitiveSearch range:NSMakeRange(0,1)] == NSOrderedSame){
            NSLog(@"YES");
        }else{
            NSLog(@"NO");
        }
}

@end