检查NSString是否以字符串结尾

Pau*_*len 1 cocoa objective-c nsstring

我想检查我的NSString是否以@ 2x.png结尾(不区分大小写).我怎样才能做到这一点?

我现在有这个代码,但似乎忽略了@ 2x部分.

    NSString *fileName = [_sourcePath lastPathComponent];
    [fileName stringByReplacingOccurrencesOfString:@"@2x" withString:@""];
    [fileName stringByReplacingOccurrencesOfString:@"@2X" withString:@""];
    NSLog(@"Filename: %@", fileName);
Run Code Online (Sandbox Code Playgroud)

2012-01-01 21:00:55.600 NewApp [23930:707]文件名:myfile@2x.png

mvd*_*vds 7

尝试这样的事情:

NSRange range = [fileName rangeOfString:@"@2x.png" options:NSCaseInsensitiveSearch];

if ( range.location != NSNotFound &&
     range.location + range.length == [fileName length] )
{
    NSLog(@"%@ ends with @2x.png",fileName);
}
Run Code Online (Sandbox Code Playgroud)

PS.请注意,您应该确保在调用之前fileName没有,因为您正在调用一个不返回的方法.在返回时调用方法,该方法与类型不兼容,并将导致意外结果.nilrangeOfStringidnilnilNSRange