Share source code of my app

I’ve uploaded source code of my app on Github under MIT license.

App and code is free, you can do with it where you want.
If you will want add some improvements for app, I may will release them in next version ;)

Thanks

P.S.: Code not cleaned and lack of comments.

Simple way to implement multi-thread

I want show you simple way to create multi-thread calculations in few strings of code.

Simple collection of operations. But remember about blocks specifications and use proper storage type.

1
2
3
4
5
6
7
NSMutableArray *operations = [NSMutableArray array];
    for (...) {
        NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
           // calculation
        }];
        [operations addObject:operation];
    }

If you don’t like blocks, you can easily make a subclass of NSOperation and overwrite main function.

Now time to create queue for operations, so just create instance of NSOperationQueue.
And in maxConcurrentOperationCount property you need setup proper value. For example 2, if you want to use just 2 concurrent threads for your calculations and continue in normal state with others operations in another threads.
In this case waitUntilFinished should be NO.

But if you want to use max threads of CPU, setup NSOperationQueueDefaultMaxConcurrentOperationCount to maxConcurrentOperationCount and YES to waitUntilFinished. Like in example below:

1
2
3
4
5
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue setMaxConcurrentOperationCount:....];
    [queue addOperations:operations waitUntilFinished:YES];
    [queue waitUntilAllOperationsAreFinished];
    [operations removeAllObjects];

If this is set, it will choose an appropriate value based on the number of available processors and other relevant factors.

1
2
3
enum {
  NSOperationQueueDefaultMaxConcurrentOperationCount = -1
};

NSOperationQueueDefaultMaxConcurrentOperationCount: The default maximum number of operations is determined dynamically by the NSOperationQueue object based on current system conditions.

That it! It’s so simple today.

You need to know something …

Objective-c
Timing for different conditions in 2147483647 (max int) iterations cycle:

1
2
3
4
5
6
7
8
9
2013-05-30 15:09:26.719 self[10228:707] object is not nil 3.580853 sec
2013-05-30 15:09:38.837 self[10228:707] object check bool 12.118355 sec
2013-05-30 15:09:51.900 self[10228:707] object check BOOL 13.061721 sec
2013-05-30 15:10:03.228 self[10228:707] object has object 11.327167 sec
2013-05-30 15:10:13.916 self[10228:707] object has object which is not nil 10.687253 sec
2013-05-30 15:10:31.121 self[10228:707] object has 3 level of objects 17.203815 sec
2013-05-30 15:10:51.921 self[10228:707] object isKindOfClass 20.798705 sec
2013-05-30 15:10:56.361 self[10228:707] perform block 4.439318 sec
2013-05-30 15:11:02.305 self[10228:707] block is available and perform then 5.943721 sec

So, isKindOfClass will slow down your app much as possible! ;)

Continue reading

How to extend existing method

With blocks it’s more easy if you need extend your method. But if you will need extend some method of another class, not yours, and you will not be able to get the sources then this solution for you. (And if you will not be able or does not have any reason for creating a subclass)

1. You need create a category of class

2. import runtime in implementation file (.m)

1
#import

3. implement your method inside category, for example :

1
2
3
4
5
6
7
8
9
- (void) extend_method {

// your code

//  here will be performed the original method
    [self extend_method];
   
// your code
}

It looks like this method has recursive calls itself, but it’s not true. Look next step

4. add method for replace (you can use +initialize or +load)

1
2
3
4
5
+ (void) initialize {
    Method original = class_getInstanceMethod(self, @selector(method));
    Method replacement = class_getInstanceMethod(self, @selector(extend_method));
    method_exchangeImplementations(original, replacement);
}

Done!

Custom UINavigationBar

Create image for navigation background – portrait
UIImage *NavigationPortraitBackground = [[UIImage imageNamed: @"navigationbarPortrait.png"]
resizableImageWithCapInsets: UIEdgeInsetsMake(0, 0, 0, 0)];

Create image for navigation background – landscape
UIImage *NavigationLandscapeBackground = [[UIImage imageNamed: @"navigationbarLandscape.png"]
resizableImageWithCapInsets: UIEdgeInsetsMake(0, 0, 0, 0)];

Set the background image all UINavigationBars
[[UINavigationBar appearance] setBackgroundImage: NavigationPortraitBackground
forBarMetrics: UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage: NavigationLandscapeBackground
forBarMetrics: UIBarMetricsLandscapePhone];

Set attributes for buttons and title
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];

[attributes setValue: [UIColor colorWithRed: 0.96f green:0.91f blue:0.64f alpha:1.00f] forKey: UITextAttributeTextColor];
[attributes setValue: [UIColor clearColor] forKey: UITextAttributeTextShadowColor];
[attributes setValue: [NSValue valueWithUIOffset: UIOffsetMake(0.0, 0.0)] forKey: UITextAttributeTextShadowOffset];
[[UIBarButtonItem appearance] setTitleTextAttributes: attributes forState: UIControlStateNormal];

[[UINavigationBar appearance] setTitleTextAttributes: attributes];

How to convert a number to the cash equivalent

NSNumberFormatter* moneyFormatter = [[[NSNumberFormatter alloc] init] autorelease];

[moneyFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];

NSString* moneyString = [moneyFormatter stringFromNumber: [NSNumber numberWithInt: 1000]];

Result: $1,000.00

Disable ARC for a single file in a project

It is possible to disable ARC for individual files by adding the

1
-fno-objc-arc

compiler flag for those files.

You add compiler flags in Targets -> Build Phases -> Compile Sources. You have to double click on the right column of the row under Compiler Flags. You can also add it to multiple files by holding the cmd button to select the files and then pressing enter to bring up the flag edit box.

Continue reading

1 2 3 9 Next »