Monday, July 18, 2011

What's New in iOS 5 sdk

The key developer-related features introduced in iOS 5.0 (Official doc):

1. iCloud

2. Storyboard

Storyboard is not xib. "A storyboard file captures your entire user interface in one place and lets you define both the individual view controllers and the transitions between those view controllers." I think It sounds like Keynote or Powerpoint. You can design every page's content and the transition between pages.

3. Newsstand (for magazines and newspapers)

4. New Frameworks

- GLKit (Game Library Kit?). The GLKit framework (GLKit.framework) contains a set of Objective-C based utility classes that simplify the effort required to create an OpenGL ES 2.0 application.

- Core Image. A powerful set of built-in filters for manipulating video and still images, such as touching up, correcting photos, face and feature detection.

- Twitter.

- Accounts. A single sign-on model for certain user accounts. Apps can use it to access twitter account.

- Generic Security Services.

5. Automatic Reference Counting (ARC).

Actually, it is a new feature of the language and the compiler. It helps you manage the objects lifetime and memory. There are some restrictions to use ARC:

1. You don't need to (and are not allowed to) call retain/release/autorelease.

2. You don't need to (but can) implement your dealloc method.

3. You can't use NSAutoreleasePool objects. Actually, they are replaced by @autoreleasepool keyword.

3. You can't define object points in C structures.

4. You can't directly cast between object and nonobject types (for example, between id and void*).

For more information about ARC itself, see Programming With ARC.

Showing an animation when NSView loading

An UIView of one of my iOS app shows a fade-in animation. I implemented this animation in viewDidLoad. When I was porting this app onto Mac OS X, I found there is no viewDidLoad.

I tried adding the fade-in animation in awakeFromNib, but no animation appeared.

My solution: Put the animation code in a method and call it after a short delay.

[self performSelector:@selector(animationShowButton) withObject:nil afterDelay:0.2];

Implementing Fade-In Fade-Out animation in NSView on Mac OS X

It is easy to implement Fade-In animation in UIView on iOS. We just need to change alpha value in the animation.

theView.alpha = 0.0f;

[UIView beginAnimations:@"fadeIn" context:nil];

[UIView setAnimationDuration:1.0];

theView.alpha = 1.0f;

[UIView commitAnimations];

But the following code does not work in NSView on Mac Snow leopard.

[theView setAlphaValue:0.0f];

[[theView animator] setAlphaValue:0.8f];

I found addSubView and removeFromSuperview can have fade-in fade-out effects.

[[theSuperview animator] addSubview:theView];

There is a problem. We cannot change the duration time in the following code:

CABasicAnimation *animation = [CABasicAnimation animation];

animation.duration = 3.0f;

[theSuperview setAnimations:[NSDictionary dictionaryWithObjectsAndKeys:animation, @"FadeIn", nil]];

[[theSuperview animator] addSubview:theView];

Now I have the solution.

Fade-in animation:

[NSAnimationContext beginGrouping];

[[NSAnimationContext currentContext] setDuration:2.0];

[[theSuperview animator] addSubview:theView];

[NSAnimationContext endGrouping];

Fade-out animation:

[NSAnimationContext beginGrouping];

[[NSAnimationContext currentContext] setDuration:2.0];

[[theView animator] removeFromSuperview];

[NSAnimationContext endGrouping];

Change the parameter order in Objective-C formatting string

(I got this idea from cocoa programming for mac os x 3rd)

When we do localizations, the problem is the words order of a language differs to other languages. For example, this sentence "Ted wants a scooter." may be something like "A Scooter is what Ted wants" in another language.

You can localize strings this way:

NSString * theFormat = NSLocalizedString(@"WANTS", @"%@ wants a %@"); x = [NSString stringWithFormat:theFormat, @"Ted", @"Scooter"];

It works well in a language:

"WANTS" = "%@ wants a %@";

But we need to change the token order this way in another language:

"WANTS" = "A %2$@ is what %1$@ wants".

Thursday, June 30, 2011

WWDC2011 Session 323 Introducing Automatic Reference Counting

Summary: Automatic Reference Counting (ARC) dramatically simplifies memory management in Objective-C. This session introduces what ARC is, how it is implemented and how to migrate to ARC. Apple asks every developer to move Objective-C code to ARC.

Problems of memory management:

Many (new) developers do not understand reference counting. They don’t know when to retain/release/autorelease. Memory leaks and the app crashes.

What is ARC?

ARC is automatic object memory management. The compiler adds retain/release calls. It still uses reference counting. ARC is not Garbage collection. ARC is compile-time memory management, not run-time memory management.

If we write a stack, our code without ARC will be:

@implementation Stack { NSMutableArray *_array; }

- (id) init {

if (self = [super init])

_array = [[NSMutableArray array] retain];

return self;

}

- (void) push: (id) x {

[_array addObject: x];

}

- (id) pop {

id x = [[_array lastObject] retain];

[_array removeLastObject];

return [x autorelease];

}

- (void) dealloc { [_array release]; [super dealloc]; }

@end

With ARC:

@implementation Stack { NSMutableArray *_array; }

- (id) init {

if (self = [super init])

_array = [NSMutableArray array];

return self;

}

- (void) push: (id) x {

[_array addObject: x];

}

- (id) pop {

id x = [_array lastObject];

[_array removeLastObject];

return x;

}

@end

How do you switch?

WWDC2011 Learning Notes and Index

WWDC2011videos come out before I finish WWDC2010 videos.

It's time to learn WWDC2011 videos. :-)

Index

WWDC2011 Session 323 Introducing Automatic Reference Counting