NSString "nil or empty" check -- is this complete? - objective-c

I was writing a small Category on NSString, and I wanted to know if this method is accurately handles all potential use cases:
Update: to clarify -- I wanted to make sure I'm not missing some oddball case involving character encodings, etc..
#implementation NSString (Helpers)
+(BOOL)stringIsNilOrEmpty:(NSString*)aString {
if (!aString)
return YES;
return [aString isEqualToString:#""];
}
#end
Sample usage:
-(void) sampleUsage {
NSString *emptyString = #"";
NSString *nilString = nil;
NSAssert([NSString stringIsNilOrEmpty:nilString] == YES, #"String is nil/empty");
NSAssert([NSString stringIsNilOrEmpty:emptyString] == YES, #"String is nil/empty");
}
#end

I only use the next conditional and do not even need a category:
if (!aString.length)
{
...
}
Using Objective-C theory, a message to NIL will return nil or zero, so basically you do not have to test for nil.

You can simplify the code by removing conditional:
+(BOOL)stringIsNilOrEmpty:(NSString*)aString {
return !(aString && aString.length);
}

#dasblinkenlight's answer is fine, but a much more readable conditional check I would use is:
NSString *string = ...; // define the string
if ([string length] == 0) {
// Do stuff with the string
} else {
// The string is empty or nil here
}
Very concise and does not require a separate convenience function definition. It's easy enough to remember.
EDIT: #Michael G. Emmons posted this as the last comment to that answer... credit to him but I'm listing this as an answer in its own right.

Some examples of this sort of "is not empty or blank" tests as a category on NSString.
// Please note that in general I advocate using a prefix on category methods
// to avoid category collisions. I've not done this here for clarity.
// The #interface is also excluded from this example for brevity.
#implementation NSString (MyAdditions)
- (BOOL)isNotEmpty
{
return [self length] != 0;
}
- (BOOL)isNotBlank
{
if ([self isNotEmpty])
{
NSCharacterSet *nonWhitespaceSet = [[NSCharacterSet whitespaceAndNewlineCharacterSet] invertedSet];
NSRange range = [self rangeOfCharactersFromSet:nonWhitespaceSet];
return range.location != NSNotFound;
}
return NO;
}
#end

Simply Check your string length
> if (!yourString.length){
> //your code } a
message to NIL will return nil or 0, so no need to test for nil :).
Happy coding ...

Make sure to check for spaces, trim white spaces before calculating length.
+(BOOL)stringIsNilOrEmpty:(NSString*)aString {
return !aString || [[aString stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]] length] == 0;
}

Related

Building 3 methods in Objective C

I'm having trouble with a school problem in Objective C. I need to build 3 methods. The first method tells you if someone is in a line. If nobody is in the line it tells you nobody is in the line otherwise it tells you who is in the line and it lists the names on a new line.
The second method adds names to the line.
The third method removes a name from the line and tells you who was removed.
First method:
-(NSString*)stringWithDeliLine:(NSArray*) deliLine{
NSString *empty = #"The line is currently empty.";
//Some kind of formatted string
if(deliLine == nil || [deliLine count] == 0)
{
empty;
}
else
{
//formatted string
}
//not sure how to return either empty or formatted string
}
Second Method:
-(void)addName:toDeliLine:(NSString*)name:(NSMutableArray*)deliLine{
[deliLine addObject:name];
}
The third method I was going to use removeObject but the instructions said not to use it so I have no idea where to start.I have the signature I think.
-(NSString*)serveNextCustomerInDeliLine:(NSMutableArray*)deliLine{
return nil;
}
For the first method I'm not sure why my literal string won't work in the if statement. I thought I was saying look at the array if nothing is in the array then it's the first object and show the string literal. else show some kinda of formatted string. I've tried all kinds of strings but none seem to be working so that's why I have the comment formatted string. If someone could give me a hint that would be great. I don't need the answer just a clue on what to think about. This is long post sorry.
A possible implementation can be the following. Please note that I have not testes edge cases and I wrote the code without Xcode support
#import <Foundation/Foundation.h>
#interface Line : NSObject
- (NSString*)printLine;
- (void)addCustomer:(NSString*)customer;
- (NSString*)removeCustomer:(NSString*)customer;
#end
#import "Line.h"
#interface Line ()
#property (nonatomic, strong, readwrite) NSMutableArray<NSString*> *customers;
#end
#implementation Line
- (instancetype)init {
self = [super init];
if (self) {
_customers = [NSMutableArray array];
}
return self;
}
- (NSString*)printLine {
NSUInteger count = self.customers.count;
if(count == 0) {
return #"Empty";
}
NSMutableString *descr = [NSMutableString string];
for (NSString *customer in self.customers) {
[descr appendString:[NSString stringWithFormat:#"%# ", customer]];
}
return [descr copy];
}
- (void)addCustomer:(NSString*)customer {
[self.customers addObject:customer];
}
- (NSString*)removeCustomer:(NSString*)customer {
NSUInteger index = [self.customers indexOfObject:customer];
if(index == NSNotFound) {
return [NSString stringWithFormat:#"%# not removed", customer];
}
NSString *removedCustomer = [self.customers objectAtIndex:index];
[self.customers removeObjectAtIndex:index];
return removedCustomer;
}
#end
Usage:
Line *line = [[Line alloc] init];
[line addCustomer:#"customer"];
NSLog(#"%#", [line printLine]);
NSLog(#"%#", [line removeCustomer:#"customer"]);
NSLog(#"%#", [line printLine]);
Edit:
I've updated my answer, passing the array as a parameter is not necessary, just initialize deliLine as a mutable array property.
For you first method, you could do the following,
- (NSString *)deliLineContents {
NSString *empty = #"The line is currently empty.";
NSMutableString *namesInQueue = [[NSMutableString alloc] init];
if(self.deliLine == nil || [self.deliLine count] == 0) {
return empty;
} else {
// Loop through your array and return a string of all the names
for (NSString *string in self.deliLine ) {
[namesInQueue appendString:string];
}
}
return [NSString stringWithString:namesInQueue];
For your second method, you're already pretty much there, maybe look up how to construct method signatures.
- (void)addNameToDeliLine:(NSString*)name {
[self.deliLine addObject:name];
}
For your third method, not sure if this meets your requirement, if not let me know.
- (NSString *)customerRemovedFromLine {
// I've making an assumption that you want to remove the first customer
NSString *servedCustomer = [self.deliLine objectAtIndex:0];
[self.deliLine removeObjectAtIndex:0];
return servedCustomer;
}
You probably don't need to pass deliLine around, just create it as a property and access it with self.deliLine. Anyway hope this helps, good luck.

Current text selection in CustomKeyBoardExtension

I'm trying to write Custom Keyboard Extension.
I'm looking for the way to know where the cursor is on UITextField,UITextView...etc in CustomKeyboardExtension ... but I don't see anything like that.
I saw SwiftKey app (http://swiftkey.com) can do that (or do something like that). When I change the cursor, suggestion-text will change (see below pictures).
Q: How can we get current text selection?
...
UPDATE: 29/09/2014
Ok, I'm so foolish. We can use documentContextBeforeInput, documentContextAfterInput methods of textDocumentProxy property. I thought that "Before","After" are about the time. Actually it's about the position.
Sorry all! I wasted your time :(
Create lastWordBeforeInput method...
-(NSString *) lastWordBeforeInput{
NSArray *arrayOfSplitsString = [self.textDocumentProxy.documentContextBeforeInput componentsSeparatedByString:#" "];
int countIndex = arrayOfSplitsString.count - 1;
NSCharacterSet *ChSet = [NSCharacterSet alphanumericCharacterSet];
NSCharacterSet *invertedChSet = [ChSet invertedSet];
while (countIndex > 0) {
NSString *lastWordOfSentance = [arrayOfSplitsString objectAtIndex:countIndex--];
if ([[lastWordOfSentance stringByTrimmingCharactersInSet:invertedChSet] rangeOfCharacterFromSet:ChSet].location != NSNotFound) {
return [lastWordOfSentance stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
}
return #"";
}
Then call it with textWillChange/textDidChange as per requirement.
- (void)textWillChange:(id<UITextInput>)textInput {
// The app is about to change the document's contents. Perform any preparation here.
NSLog(#"%#",[self lastWordBeforeInput]);
}
Hope this will help you.

Matching strings, consider some characters are the same

please help me with this problem.
I want to check if the targetString match the keyword or not. Consider some character may different, but should still return true.
Example:
targetString = #"#ß<"
keyword = #"abc", #"∂B(", #"#Aß<"
result: all must return true.
(Matched.targetString and all keyword are the same.)
Consider me have an array, contains list of character set that can be the same:
NSArray *variants = [NSArray arrayWithObjects:#"aA#∂", #"bBß", #"c©C<(", nil]
So that when matching, with this rule, it can match as the example above.
Here is what i've done so far (using recursion):
- (BOOL) test:(NSString*)aString include:(NSString*) keyWord doTrim:(BOOL)doTrim {
// break recursion.
if([aString length] < [keyWord length]) return false;
// First, loop through each keyword's character
for (NSUInteger i = 0; i < [keyWord length]; i++) {
// Get #"aA#∂", #"bBß", #"c©C<(" or only the character itself.
// like, if the keyword's character is A, return the string #"aA#∂".
// If the character is not in the variants set, eg. P, return #"P"
char c = [keyWord characterAtIndex:i];
NSString *rs = [self variantsWithChar:c];
// Check if rs (#"aA#∂" or #"P") contains aString[i] character
if([rs rangeOfString:[NSString stringWithCharacters:[aString characterAtIndex:i] length:1]].location == NSNotFound) {
// If not the same char, remove first char in targetString (aString), recursion to match again.
return [self test:[aString substringFromIndex:1] include:keyWord doTrim:NO];
}
}
// If all match with keyword, return true.
return true;
}
- (NSString *) variantsWithChar:(char) c {
for (NSString *s in self.variants) {
if ([s rangeOfString:[NSString stringWithFormat:#"%c",c]].location != NSNotFound) {
return s;
}
}
return [NSString stringWithFormat:#"%c", c];
}
The main problem is, variantsWithChar: doesn't return the correct string. I don't know which datatype and which function should I use here. Please help.
For thou who know ruby, here's the example in ruby. It work super fine!
require 'test/unit/assertions'
include Test::Unit::Assertions
class String
def matching?(keyword)
length >= keyword.length && (keyword.chars.zip(chars).all? { |cs| variants(cs[0]).include?(cs[1]) } || slice(1, length - 1).matching?(keyword))
end
private
VARIANTS = ["aA#∂", "bBß", "c©C<("]
def variants(c)
VARIANTS.find { |cs| cs.include?(c) } || c
end
end
assert "abc".matching?("#ß<")
PS: The fact is, it's containt a japanese character set that sounds the same (like あア, いイ... for thou who know japanese)
PS 2: Please feel free to edit this Question, since my engrish is sooo bad. I may not tell all my thought.
PS 3: And, maybe some may comment about the performance. Like, search about 10,000 target words, with nearly 100 variants, each variant have at most 4 more same characters.
So first off, ignore comments about ASCII and stop using char. NSString and CFString use unichar
If what you really want to do is transpose hiragana and katakana you can do that with CFStringTransform()
It wraps the ICU libraries included in OS X and iOS.
It makes it very simple.
Search for that function and you will find examples of how to use it.
After a while (a day) working on the code above, I finally get it through. But don't know about the performance. Someone comment and help me improve about performance, please. Thanks.
- (BOOL) test:(NSString*)aString include:(NSString*) keyWord doTrim:(BOOL)doTrim {
// break recursion.
if([aString length] < [keyWord length]) return false;
// First, loop through each keyword's character
for (NSUInteger i = 0; i < [keyWord length]; i++) {
// Get #"aA#∂", #"bBß", #"c©C<(" or only the character itself.
// like, if the keyword's character is A, return the string #"aA#∂".
// If the character is not in the variants set, eg. P, return #"P"
NSString* c = [NSString stringWithFormat:#"%C", [keyWord characterAtIndex:i]];
NSString *rs = [self variantsWithChar:c];
NSString *theTargetChar = [NSString stringWithFormat:#"%C", [aString characterAtIndex:i]];
// Check if rs (#"aA#∂" or #"P") contains aString[i] character
if([rs rangeOfString:theTargetChar].location == NSNotFound) {
// If not the same char, remove first char in targetString (aString), recursion to match again.
return [self test:[aString substringFromIndex:1] include:keyWord doTrim:NO];
}
}
// If all match with keyword, return true.
return true;
}
If you remove all comment, it'll be pretty short...
////////////////////////////////////////
- (NSString *) variantsWithChar:(NSString *) c{
for (NSString *s in self.variants) {
if ([s rangeOfString:c].location != NSNotFound) {
return s;
}
}
return c;
}
You could try comparing ascii values of the japanese characters in the variants's each character's ascii value. These japanese characters aren't treated like usual characters or string. Hence, string functions like rangeOfString won't work on them.
to be more precise: have a look at the following code.
it will search for "∂" in the string "aA#∂"
NSString *string = #"aA#∂";
NSMutableSet *listOfAsciiValuesOfString = [self getListOfAsciiValuesForString:string]; //method definition given below
NSString *charToSearch = #"∂";
NSNumber *ascii = [NSNumber numberWithInt:[charToSearch characterAtIndex:0]];
int countBeforeAdding = [listOfAsciiValuesOfString count],countAfterAdding = 0;
[listOfAsciiValuesOfString addObject:ascii];
countAfterAdding = [listOfAsciiValuesOfString count];
if(countAfterAdding == countBeforeAdding){ //element found
NSLog(#"element exists"); //return string
}else{
NSLog(#"Doesnt exists"); //return char
}
===================================
-(NSMutableSet*)getListOfAsciiValuesForString:(NSString*)string{
NSMutableSet *set = [[NSMutableSet alloc] init];
for(int i=0;i<[string length];i++){
NSNumber *ascii = [NSNumber numberWithInt:[string characterAtIndex:i]];
[set addObject:ascii];
}
return set;
}

what character to use to be the last in string comparison in objective c?

In my app I read contacts from address book, but for some of them, the name fields is empty. I want to put them at the end of my list by using a special string. What sort of special string should I use to make sure when the array is getting sorted ascending, person with missing name is placed at the end of the array?
for example my name array is
array = #[#"Andy", #"Brad", #"Zoro", #"[missing name]"];
[array sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
I have tried using <UNKNOWN NAME> or [UNKNOWN NAME] or <UNKONWN NAME> but they all turned out to be in earlier position than Andy
Thank you in advance.
Conventional
Don't use a special string for missing names; don't do unnatural things just because you are missing data. Write your own message that uses localizedCaseInsensitiveCompare:, and put it in your own category on NSString.
// NSStringEmptyLast.h
#interface NSString (EmptyLast)
- (NSComparisonResult)localizedCaseInsensitiveCompareEmptyLast:(NSString *)aString
#end
// NSStringEmptyLast.m
#import NSStringEmptyLast.h
#implementation NSString (EmptyLast)
- (NSComparisonResult)localizedCaseInsensitiveCompareEmptyLast:(NSString *)aString {
if ([self length] == 0 && [aString length] == 0) {
return NSOrderedSame;
} else if ([self length] == 0 && [aString length] != 0) {
return NSOrderedDescending;
} else if ([self length] != 0 && [aString length] == 0) {
return NSOrderedAscending;
} else {
return [self localizedCaseInsensitiveCompare:aString];
}
}
#end
Then, use:
[array sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompareEmptyLast:)];
Don't be afraid to add messages for existing classes.
Blocks
Or, if you don't want to write a category, use
- (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr
where cmptr is a NSComparator block, which should have the equivalent of the message given above.
Try using curly brackets: {…}, pipe: |, or tilde: ~.
Here is what I ended up with. Since I want to avoid array sorting after query from the database. I setup a fake string to people that doesn't have name. This might not be the most elegant answer but it will make people with this name appear last if I query from core data using name in ascending order.
// Person+UnknownName.h
#interface Person (UnknownName)
+ (NSString)unknownName
#end
// Person+UnknownName.m
#import "Person+UnknownName.h"
#implementation Person (UnknownName)
+ (NSString)unknownName {
unichar lastcharacter = 0xffffffff;
NSString * UNKNOWN_NAME = [NSString stringWithCharacters:&lastcharacter length:1];
}
#end

Objective-C String Function: Contains String

How can I check if an NSString contains another substring, at which point it will return a Boolean Value.
This is what I'm thinking of:
If myString.contains("string") then
{
//Stuff Happens
}
But, from the research I've done, it seems as if Obj-C has no function for this. This Wikipedia article gives numerous string functions, their differences, and all in different languages, but I see no Obj-C support for any Contain Function.
Does anyone know of a simple-to-use function like the once above (which is similar to the C# and VB.NET function)?
Would a "Find" Function work? If so, how?
If this is not supported in Obj-C, is there a workaround I can use?
Any help is very appreciated.
if ([myString rangeOfString:#"string"].location != NSNotFound)
{
// Stuff happens
}
NSString *someString = #"Time for an egg hunt";
if ( [someString rangeOfString:#"egg" options:NSCaseInsensitiveSearch].location != NSNotFound ) {
NSLog( #"Found it!" );
}
If you want to be case insensitive.
NSRange range = [string rangeOfString:#"string" options:NSCaseInsensitiveSearch];
if (range.location != NSNotFound)
{
return range.location;
}
else
{
return nil;
}
Documentation
Create a NSString category, and put that in...
Code :
- (BOOL)contains:(NSString *)str
{
NSRange aRange = [self rangeOfString:str];
return (aRange.location!=NSNotFound);
}
Usage :
NSString* testStr = #"This is my string";
if ([testStr contains:#"is"])
{
// do something
}
if([string rangeOfString:substring].length > 0)
...