Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

case insensitive checking of suffix of NSString

I would like to check if a string has a certain suffix, but would like to do a case insensitive checking. I'd need an alternative for this:

if ([selectedFile hasSuffix:@"jpg"] ||
    [selectedFile hasSuffix:@"JPG"]) 

or jPg, jpG, Jpg. Is there any short way to do this? I don't see how I could apply caseInsensitiveCompare: in this situation.

like image 283
n.evermind Avatar asked Aug 20 '12 20:08

n.evermind


People also ask

Is NSString UTF 8?

An NSString object can be initialized from or written to a C buffer, an NSData object, or the contents of an NSURL . It can also be encoded and decoded to and from ASCII, UTF–8, UTF–16, UTF–32, or any other string encoding represented by NSStringEncoding .

What is NSString in Swift?

NSString : Creates objects that resides in heap and always passed by reference. String: Its a value type whenever we pass it , its passed by value. like Struct and Enum, String itself a Struct in Swift.

What is NSString in objective C?

(NSString *) is simply the type of the argument - a string object, which is the NSString class in Cocoa. In Objective-C you're always dealing with object references (pointers), so the "*" indicates that the argument is a reference to an NSString object.


2 Answers

If selectedFile is a filename, it's best to look at the entire extension. It's unlikely but possible that you'll find a file titled ThisIsNotAnImage.asdfjpg. If you just check the suffix, you'll incorrectly conclude that this is an image.

Fortunately NSString has many methods for working with paths, including pathExtension.

Also, do you want to always and only accept JPEG images, or all images that you can load? Most of Apple's image classes support an impressive range of file formats.

If you're writing for iOS, the image formats recognised by UIImage are listed in the UIImage Class Reference, along with their extensions.

If you're writing for Mac OS, NSImage has a class method called imageFileTypes, allowing the formats supported to change at run time.

As noted in the UIImage Class Reference, JPEG files sometimes have the extension .jpeg. If you're manually looking for JPEGs, you should check for both .jpg and .jpeg.

Testing for JPEGs only

NSString *extension = [selectedFile pathExtension];
BOOL isJpegImage = 
    (([extension caseInsensitiveCompare:@"jpg"] == NSOrderedSame) || 
     ([extension caseInsensitiveCompare:@"jpeg"] == NSOrderedSame));

if (isJpegImage)
{
    // Do image things here.
}

Testing for everything UIImage can load

NSString *loweredExtension = [[selectedFile pathExtension] lowercaseString];
// Valid extensions may change.  Check the UIImage class reference for the most up to date list.
NSSet *validImageExtensions = [NSSet setWithObjects:@"tif", @"tiff", @"jpg", @"jpeg", @"gif", @"png", @"bmp", @"bmpf", @"ico", @"cur", @"xbm", nil];
if ([validImageExtensions containsObject:loweredExtension])
{
    // Do image things here.
}

Testing for everything NSImage can load

NSString *loweredExtension = [[selectedFile pathExtension] lowercaseString];
NSSet *validImageExtensions = [NSSet setWithArray:[NSImage imageFileTypes]];
if ([validImageExtensions containsObject:loweredExtension])
{
    // Do image things here.
}
like image 125
Cowirrie Avatar answered Oct 15 '22 12:10

Cowirrie


[[selectedFile lowercaseString] hasSuffix:@"jpg"]
like image 32
mipadi Avatar answered Oct 15 '22 10:10

mipadi