Wednesday, February 29, 2012

Tips – Using janus to manage your Vim settings.

What is janus?

From janus‘s official Github page:

This is a distribution of plug-ins and mappings for Vim, Gvim and MacVim.

Just as it statement says, it packaged some popular plug-ins and mapping for you, so you can to use a full featured Vim after you installed Vim(If you are using Mac and Lion+, that means you just need to run a command and start to use a luxury Vim).

If you are in a rush, just copy the line below and paste to your terminal.

 $ curl -Lo- | bash  

Why bothering janus?

There are many famous plug-ins for Vim. But searching and installing plugins could be a painful work.

Usually, you google it, then go to to download it, then you config it and use it. This process is fine for one plugin, what if you want to install the top 10 vim plugins for coder, what if you want to install another 10 popular plugins?

For me, I can’t go through this process again and again. Don’t even think about the weird errors popped out when you installing your plugins.

What’s the merit I can get?

With janus, all you need to know is run a command and then use it. Oh, maybe you still need to remember some command for the plugins.

Where to start?

If you have enough patient to read to here, I suggest you to take a look at janus’s Github page.

What? you just want to use it? OK, open your terminal and paste the code below.

 $ curl -Lo- | bash  

Good luck!

And, don’t forget to follow my twitter @TonnyXu

Via Tonny Xu Programming, Something really cool!, Tips

Wednesday, February 22, 2012

LLVM Compiler Overview(By Apple)

LLVM is the next generation of compiler created by Apple, intended to replace the whole GCC-family tools. It’s really, really awesome!

Here is an overview guide provided by Apple, but it hasn’t been updated for over 1 year. And during 2011, Apple released LLVM 3.1

NOTE: This page is the official release page, and will always show the most recent release. So if you visit this page and see LLVM 4.x, don’t be surprised.

Any way, it’s a good source for understanding the low level tool chains used by Apple and how apple is changing the C family compiler.

The documentations

Go to DevCenter, this page is free, no login is necessary. And all the videos has transcripts!

Other videos

Actually, the Clang community has some regular events held in San Francisco or San Jose. You can check it out from LLVM’s official site, for example, LLVM Developers’ Meeting@Nov 2011

And, as always, don’t forget to follow me @TonnyXu

Via Tonny Xu iPhone Dev, Programming

Tuesday, February 21, 2012

ARC Learning Notes(1)

ARC(Automatic Reference Counting) was introduced by Apple on WWDC 2011, along with iCloud and OS X Lion. It’s a wonderful feature we need, but, how much it has been adopted to real projects?

How many projects adopted ARC?

Currently, 0 project is using ARC in my company.

This does not mean we hadn’t started new project since ARC was released, but because we have some constraints. We are using some common libraries across our projects, and we need to maintain the libraries consistent. We also need to add many features to the apps we released, and also, using ARC means all of our team members need to learn how to use ARC and know the common practices and have a better understanding of memory usage. That’s the time we could not afford in the past 8 months.

But we know we will move to ARC inevitably. So the problem is when?

Status of My projects

Currently we started to use ARC in some test projects. Here the Test Project means we use such kind of project to test some features quickly and without infecting the real projects.

What about yours?

How about yours? Are you starting to use ARC in your project now? Tell me about your projects’ status in the comment please.

It’s the time!

After 8 months, by watching a lot of Open Source project adopting ARC, I think it is our time to start using ARC from now on.

ARC only needs the new compiler, and it has a good backward compatibility to iOS 4. So let’s just try it.

Start from where?

Where should we start from? I think we can start from 2 things.

Apple’s document

Apple’s DevCenter for iOS has a good document for using ARC. Here is a simple list as far as I know.

  1. What’s New in iOS 5
  2. Transitioning to ARC Release Notes
  3. Advanced Memory Management Programming Guide
  4. Memory Management Programming Guide for Core Foundation

WWDC 2011′s HD Video

Also, WWDC 2011′s videos are great, don’t miss this one.

  1. Session 323 – Introducing Automatic Reference Counting

Next time, I will post what I learned about ARC.

And as always, don’t forget to follow me @TonnyXu

Via Tonny Xu iPhone Dev, Programming

Saturday, February 11, 2012

Using purge command in Mac

Have you ever heard about the command purge? Hmmm, never heard about it? Well, that’s means you are view the right page.

Let me change the question.

Have you ever met a situation like this?

8GB, but full figure 1

Holy shit, 8GB DDR3 memory is still not enough?


Most of the time, I will blame my default browser: chrome! Usually, when I quit chrome, it will free 3GB+ memory, and my Lion system will go smoothly again.

But, chrome is not always the reason of high consumption of my physical memory. I don’t know which one is using a huge bunch of memory then leave it as INACTIVE, just as figure 1 looks like. The blue pie is huge! and the most important thing is it is INACTIVE.

What’s next?

OK, here purge comes to rescue, open your terminal, then type:

 %: purge  

BOOOOM! my system looks like as below, and feels much more faster!

after using purge

What happened?

The story behind the scene is relatively complicated, you can read Apple’s document. But anyway, it’s a shortcut to free your valuable INACTIVE memory.

Don’t forget to follow me @TonnyXu

Via Tonny Xu Something really cool!, Tips

Friday, February 10, 2012

Using sed command in Mac OS X

It’s easy to use sed command, right?

 %: sed -i "s/old/replace/g" mysql.txt  

This tip is all around the net. But! it doesn’t work with Mac!

WTF? How to make it work?

Try it yourself, I’m using Lion 10.7.3, it cost me over 30 minutes to search and read the man page.

-i extension

Edit files in-place, saving backups with the specified extension. If a zero-length extension is given, no backup will be saved. It is not recommended to give a zero-length extension when in-place editing files, as you risk corruption or partial content in situations where disk space is exhausted, etc.

What is a zero-length extension? Try this:

 %: sed -i "" "s/old/replace/g" mysql.txt  

The empty string behind -i as -i "" is the KEY point.

Replace in multiple files

Try this:

 %: find path/to/search -name "*.php" -exec sed -i "" "s/old/replace/g" {} \;  

Good luck! Don’t forget to follow me @TonnyXu

Via Tonny Xu Tips

Have you seen MIN and MAX macro in Objective-C

How will you write a MIN(a, b) or MAX(a, b) macro?

In the real world, it is easy to tell which is the minimum and which is the maximum of 2 numbers. But to write a correct function in computer, it’s not an easy job as we can see.

Usually, MIN and MAX functions are written in macro, so how will you write it?

 #define MYMIN1(a, b) {a < b ? a : b;}  

This is definitely wrong, at least you need to add the parenthesis to the parameters. OK, it’s easy, let’s add this:

 #define MYMIN2(a, b) ({(a) < (b) ? (a) : (b);})  

But, this is still wrong. Let’s take a look at what Objective-C and other languages defines these 2 macros.

How does Objective-C define MIN and MAX?

Because there is no MIN or MAX defined by C standard library, Objective-C defined this in NSObjCRuntime.h

Let’s see the code:

 #if !defined(MIN) #define MIN(A,B)    ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __a : __b; }) 

Thursday, February 2, 2012

CoreData with NSOrderedSet does not work well on insertion

Usually, we generate an entity class with Editor->Create NSManagedOBject Subclass... For example we created a subclass of NSManagedObject

Xcode generates the .h file like this:

 @interface MyEntity : NSManagedObject  @property (nonatomic, retain) NSOrderedSet manySubs;  //... some methods generated by Xcode - (void)addManySubsObject:(MySubEntity*)value; //... some other methods generated by Xcode  @end  

and the .m file like this:

 @implement MyEntity  @dynamic manySubs;  @end  

If you use MyEntity just like this, you will get an error when you try to insert a new MySubEntity class by calling [myEntityObj addManySubsObject:aSubEntityObj];.

The error reads:

*** -[NSSet intersectsSet:]: set argument is not an NSSet

This is a bug Apple hasn’t resolved yet, but you can work around this bug by adding the following method to .m file.

 - (void)addManySubsObject:(MySubEntity*)value{     [self willChangeValueForKey:@"manySubs"];     NSMutableOrderedSet *tempSet = [NSMutableOrderedSet orderedSetWithOrderedSet:self.manySubs];     [tempSet addObject: value];     self.manySubs = tempSet;     [self didChangeValueForKey:@"manySubs"]; }  

Be careful

Via Tonny Xu iPhone Dev, Programming