Hello my friend, this is my first time I have written blog about iOS. With 2 year exp develop iOS app.
There are many thing I learned from ebook and internet. I think right now is the best time I should start to share everything I know to you.
In this blog, I will focus to something hide in Cocoa framework.
If you are a experienced developer iOS, even you are beginner. I think this blog is worth reading. Trust me ;]
You can extent and close easily. Making my blog is clearly.
NSLog is useful method when you wanna know object’ information when debugging.
NSLog is supported log many object in Cocoa framework like NSString, NSArray, NSDictionary,… and many class in UIKit.
But if you have custom object, when you log it.
It is just log what class your object is and your object’s address in memory. Sometime, It don’t have enough what you want.
So there is a simple way to approach it.
Just override -(NSString *) description.
Here is sample :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
@interface MyClass : NSObject @property (strong, nonautomatic) NSString *myInfo; @property (assign) CGFloat myFloat; @end // Your implementation @implementation MyClass -(NSString *) description // Override this method { return [NSString stringWithFormat:@"This is my description with custom info | myInfo = %@ , myFloat = %.2f ",_myInfo,_myFloat)]; } @end |
And your log like this :
1 |
This is my description with custom info | myInfo = NghiaTran , myFloat = 10.0 |
So, when you need more information when log custom object, you just override it simply.
NSExpression
Stand back, I’m going to try math.
1 |
NSString *mathExpression = @"3 + 5 *(12 - 6) / 100 + 2e10" |
If this is your exercises, it’s very solve it.
But Can you solve this expression by your code ?
Whoa, this is NOT easily. But in Cocoa has some special for you.
NSExpression is recuse.
1 2 3 4 5 |
NSString *mathExpression = @"3 + 5 *(12 - 6) / 100 + 2e10" NSExpression *e = [NSExpression expressionWithFormat:mathExpression, nil]; NSNumber *result = [e expressionValueWithObject:nil context:nil]; |
This class solve more than easily.
Don’t need a big program with many method, analystic in it.
This save your day.
Trick
I collect useful trick when you deal with Collection like NSArray, NSDictionary.
Reverse array quickly inline.
1 2 3 |
NSArray *numbers = @[ @1, @2, @3 ]; NSArray *reversed = numbers.reverseObjectEnumerator.allObjects; |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
typedef struct RGB { float red, green, blue; } _RBG; RGB color = {1.0 , 1.0 , 0); // Use NSValue to hold your struct NSValue *myStruct = [NSValue valueWithBytes:&color objCType:@encode(RGB)]; |
1 2 3 4 5 6 7 8 9 |
NSArray *arrString = @{@"first",@"second",@"third"}; // Convert all member in array to UpperCase [arrString valueForKey:@"uppercaseString"] // And log like this // {FIRST, SECOND, THIRD} |
NSDataDetector
In some situation, your app should detect what is useful info in a long email or note.
to
Cocoa have awesome class – NSDataDetector was built-in.
NSDataDetector can detect :
Here is example :
1 2 3 4 5 6 7 8 9 10 11 |
NSString *string = @"123 Main St. / (555) 555-5555"; NSError *error; NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink | NSTextCheckingTypePhoneNumber error:&error]; [detector enumerateMatchesInString:string options:kNilOptions range:NSMakeRange(0, [string length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) { NSLog(@"Match: %@", result); }]; |
CFStringTransform
Have ever you hear or used CFStringTransform in your app ?
This is the last Gem in Cocoa I will show you.
It will strip accents and diacritics. It will name Unicode characters. It will encode XML hex entities which is useful if you’re ever, you know, making XML or decoding XML.
And you can also transliterate between writing systems which is kind of mind-blowing whenever you see it in action.
So here it is, meet CFStringTransform.
1 2 3 4 5 6 7 8 9 10 11 |
Boolean CFStringTransform ( CFMutableStringRef string, CFRange *range, CFStringRef transform, Boolean reverse ); |
Here is the first sample :
1 2 3 4 5 |
NSMutableString *string = [@"Énġlišh låcks iñterêsţing diaçrïtičş" mutableCopy]; CFStringTransform((__bridge CFMutableStringRef)(string), NULL, kCFStringTransformStripCombiningMarks, NO); // => This string will transform to "English lack interesting diacities" |
Show what about Emoji ?
CFStringTransform can translate Emoji character to english.
1 2 3 4 5 |
NSMutableString *string = [@"<Pig Face>" mutable]; CFStringTransform((__bridge CFMutableStringRef)(string), NULL, kCFStringTransformToUnicodeName, NO); // => Pig Face |
The last gem I will show you is a ability to transform non-latin character to latin character.
If mean, if your app has some text in Japanese like “こんにちは” and you want to convert it to pronunciation for speech easily.
Sample :
1 2 3 4 5 |
NSMutableString *string = [@"こんにちは" mutableCopy]; CFStringTransform((__bridge CFMutableStringRef)(string), NULL, kCFStringTransformToLatin, NO); // => pronunciation of this word is Kon'nichiwa |
You can gain more User Experience.
So, there are many hidden gem in Cocoa and Cocoa Touch. With it, you can implement awesome featured in your iOS app.
Thank for reading ;]