Wednesday, February 22, 2012

How to read a file in Xcode project

Recently I got stuck with an objective C problem, Given a text file in your Xcode project, how could I read this text file in my Xcode 4.2 project?
For example, you got a text file called test.txt, it has some contents. In your Xcode project, you need to read its file and print its content. It sounds easy at the beginning, but end up really tough, especially you are new with the latest version of Xcode.
The key problem is you need to make sure the file test.txt is put as the same folder as the build target file, which is the Products folder, otherwise you will always get the file not existed exception.

Through several times try out, finally I figure out how to do it.

1. Add your file into project
   Select File - Add File into xxx, chose the created file into project.
2. Add a "copy file" build phase
    View - Navigator - Show Project Navigator, chose Project, select targets, select Build phase,
    and click "Add Build Phase", add "Copy files";
    in the new Copy Files build phase, choose
      Destination - "Production Directory"
     and click "+" symbol, then choose the file test.txt in your project.

How to test if the file was added in the Product folder or not?
After press the "Run" button, you will see the executable target file under the "Product" folder of your Xcode project,select the target file, right click, select "Show in Finder", then the Product Folder will pop up in the Finder Application, then you should see the test.txt file in the same folder.

Monday, February 13, 2012

Jeremy Lin - an inspring story

Last week NBA belongs to Jeremy Lin!  he just exploded to a star from an unknown underdog, leaded his team 5 wins in a row in a week! What a amazing true cinderella story! I watched his game vs. Lakers last Friday. I was really enjoying the game: he demonstrated his basketball skills and talent: he is smart, he got a high IQ in the basketball. I like his personality: confident, calm and humble. I think these will attribute with his Christian religion, Harvard graduate background and his Chinese descendent. Jeremy's successful story made me realize that Basketball can be played like this: you don't need a very strong, very tall or very fast body, if you have a regular body but you are smart enough and hardworking, you can be a good player as well, like Jeremy Lin! The IQ and skills are more important than the body.

Now I becomes his fan, his story is really inspiring, I learned:
- Always have a big dream. Don't limit your self. You don't have to be genius, you need hard working.
- Craft your skills, practice hard. Jeremy's success is not by accident. I can feel behind scene he must be a hard worker. Since those skills can not be done in a short time.
- Be patient and prepared for the opportunity, you can only seize the chance when you are prepared.

I hope his lin-sanity keeps going!

 Jeremy Lin's blog
 His facebook page

Using Groovy to update/generate XML file

Recently when I refactored the Android framework in my work, I found I need to generate eclipse .classpath file based on the library file.
I found using Groovy XML library is a lot easier to do this job compare to Java.

1. Add a new library to the current .classpath file.
Problem:  You have a Eclipse .classpath file, which is XML, you need to add another jar file in the class path. for example, adding following into the .classpath:
 If you want to write the output to file, use the following code snippet:

There is another method to print XML, using XmlNodePrinter, like:
But I found using XmlNodePrinter will not generate line break, and no xml header either.

2. Generate a new .classpath file
Problem:  iterate the libs folder, add all the jar file name into the .classpath file.