iOS Tutorials

After working on Android on a daily basis for almost 1 year, I started out my journey on iOS to discover the other major half of the mobile world. I followed a few different resources to help me understand different things on iOS dev, mainly Objective-C and the Xcode IDE. This blog post will include a list of tutorials I used and highlight what they are good for. This is to keep a note to myself of useful resources, and at the same time, I hope it will be useful to others too! 🙂

  • Official getting started guide
  • About Objective-C method and function
  • An Overview of Objective-C
    • Website: http://cocoadevcentral.com/d/learn_objectivec/
    • Remarks: Good overview of Objective-C. Also explains well on memory management. Objects creation can be confusing when you’re new to Objective-C. I saw 2 ways of making objects: “NSString* string1 = [NSString string];” and “NSString* string2 = [[NSString alloc] init];”. This guide will help you demystify it and learn more about memory management in Objective-C.

Some other guides I found but yet to finish:

That’s it for now, more to be added to this list as I study! bye!

 

Troubleshooting Postgres Error: Permission denied to create db

Today I tried to setup Postgres as my db for my Rails project, and met with this error:

Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"my_cute_db", "pool"=>5, "username"=>"rongjun", "password"=>nil}
PG::Error: ERROR:  permission denied to create database

I want to write down how I solved it.

What I intended to do is the create a db called my_cute_db with rake command, rake db:setup. However, it’s giving error like shown above.

Here’s my database.yml

development:  
adapter: postgresql  
encoding: unicode  
database: my_cute_db  
pool: 5  
username: rongjun  
password:

My username is rongjun. And the error is telling me that “permission denied to create database“, so I have to go into psql command-line interface to find out the permission of my role.

my_terminal# psql postgres
postgres=# \du
                              List of roles
 Role name  |                   Attributes                   | Member of
------------+------------------------------------------------+-----------
 rongjun    |                                                | {}
 daodaowang | Create DB                                      | {}
 kai        | Password valid until infinity                  | {}
 tanjunrong | Superuser, Create role, Create DB, Replication | {}
 validator  |                                                | {}

\du command is listing down the role names and their privileges. We can see that rongjun is not having any privileges. On the other hand daodaowang is able to Create DB. Now we would like to enable rongjun to be able to Create DB just like daodaowang.

postgres=# ALTER ROLE rongjun WITH CREATEDB;

Then we run \du again to check:

postgres=# \du
                              List of roles
 Role name  |                   Attributes                   | Member of
------------+------------------------------------------------+-----------
 rongjun    | Create DB                                      | {}
 daodaowang | Create DB                                      | {}
 kai        | Password valid until infinity                  | {}
 tanjunrong | Superuser, Create role, Create DB, Replication | {}
 validator  |                                                | {}

rongjun is now having the correct privilege to Create DB. Let’s run rake db:create again, it will do the job without error now. You can check by running psql -l command to list down all databases you have.

After creating the db, you can grant more privileges to your user:

postgres=# GRANT ALL PRIVILEGES ON DATABASE my_cute_db to rongjun;

Reference:
For how to create database refer to: http://www.cyberciti.biz/faq/howto-add-postgresql-user-account/

For creating the user with ‘Create DB’ privilege directly without having to ALTER it later:

create user any_username with password 'any_password' createdb;

Today I learned about a pretty cool feature about rails.

We can write #TODO, #OPTIMIZE and #FIXME as a comment in Rails code, then when we type rake notes in the project directory, we can see a list of file with line numbers indicating all the places we have the 3 tags.
An example output of rake notes:

     app/models/user.rb:
        * [  14] need refactoring
        * [  50] obsolete, to be deleted
        * [  96] can still be optimized

If 3 tags is not enough, we can also have our on tags, let’s say we want to have a #TO_BE_DELETED tag, just do the following in the code:

      line 63: # TO_BE_DELETED The Code Below Are Obsolete

Then do: rake notes:custom ANNOTATION=TO_BE_DELETED

output:

     app/models/user.rb:
        * [ 63] The Code Below Are Obsolete

% notation in Ruby

Today I wanna share about the %notation I learned.

Any single non-alpha-numeric character can be used as the delimiter.

1.9.3-p327 :005 > %*lala lala "hehe"*
 => "lala lala \"hehe\""
1.9.3-p327 :006 > %&lala lala "hehe"&
 => "lala lala \"hehe\""

As you can see, * worked, & worked, so other symbol and friends will work too. Above example is using % notation to produce string, but % notation is actually more powerful than that, you can use it to produce array, regexp, shell command:


%Q{one\ntwo\n#{ 1 + 2 }}
=> "one\ntwo\n3"

%q{one\ntwo\n#{ 1 + 2 }}
=> "one\\ntwo\\n#{ 1 + 2 }"

%r/#{name}/i
=> /nemo/i

%w{one two three}
=> ["one", "two", "three"]

%i{one two three} # after Ruby 2.0
=> [:one, :two, :three]

%x{ruby --copyright}
=> "ruby - Copyright (C) 1993-2009 Yukihiro Matsumoto\n"

The post is not an exhaustive guide, but an introduction. Here’s where I copied the material from: http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals#The_.25_Notation

Take a tour to % notation through the link if you haven’t, it is definitely beneficial!

If you’re familiar with test, you can try the examples written in minitest:
https://github.com/teohm/a-dip-in-ruby/blob/master/spec/percent_notation_spec.rb

It’s from this blog post: http://teohm.github.io/blog/2012/10/15/start-using-ruby-percent-notation/

It’s a tiring day… It’s time to get some rest! Good night 🙂

Fixed Grid vs. Fluid Grid

Time for a short post. Today I’m looking at how to use twitter bootstrap. I took a look at the tutorial about the grid system, so I saw that they have this fixed grid system and fluid grid system. After reading through and trying hard to find out what’s their difference on the site, I finally gave up. They really looked the same. So it’s time for some googling. Ah-ha! Lucky enough, there’s a 100+ upvoted thread in Stackoverflow discussing about this topic.

http://stackoverflow.com/questions/9780333/twitter-bootstrap-fluid-vs-fixed-then-add-in-responsive-how-does-it-all-fit-t

In short the fixed grid will not stretch itself longer even your browser width increased; while fluid grid will stretch itself.

Fixed grid example: http://twitter.github.io/bootstrap/examples/hero.html
Fluid grid example: http://twitter.github.io/bootstrap/examples/fluid.html

They even provide the raw html and css code to further explain fluid/fixed + non-responsive/responsive:

Fluid-only: http://jsfiddle.net/5JECu/
Fluid-responsive: http://jsfiddle.net/ZnEEa/
Fixed-only: http://jsfiddle.net/eterpstra/ZR4zz/3/
Fixed-responsive: http://jsfiddle.net/eterpstra/rdvaG/12/

Bye!

Ruby’s alias_method

Today I learned about an interesting method in Ruby alias_method, this method can be used to refer to an old method in a module. The documentation is here: http://apidock.com/ruby/Module/alias_method. But at first glance, I couldn’t understand what it’s used for. So this simple post will illustrate a little.

Let’s say we have a module initially:

module Mod
  def print_asdf
    puts "lalalala"
  end
end

We can use it like this:

irb> include Mod
 => Object
irb> print_asdf
  lalalala

Later we decided to reopen the module and overwrite the function to add something:

module Mod
  def print_asdf
    print_asdf
    puts "changed"
  end
end

It’s just like calling the superclass method when we’re doing inheritance in OOP, but for this case we can’t use super.print_asdf, if we call the above method, we’ll run into error:

irb> include Mod
irb> print_asdf
SystemStackError: stack level too deep
   from /Users/tanjunrong/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/irb/workspace.rb:80
Maybe IRB bug!

That’s because it’s running recursively! This can be solved by alias_method:

module Mod
  alias_method :hey, :print_asdf

  def print_asdf
    hey
    puts "hehe"
  end
end

Now we can use it properly:

irb> include Mod
irb> print_asdf
  lalalala
  hehe

Simple post. Bye!

Git Cherry Pick

Today, I learned about git cherry-pick. The man page of cherry-pick states that:

Given one existing commit, apply the change the patch introduces, and record a new commit that records it. This requires your working tree to be clean (no modifications from the HEAD commit).

One of the use case I know of is to take a commit from one branch, and apply it on another. Given this,

- a - b - c - d - e - f (master)
            \
              g - h - i (dev)

and if I do this:

git checkout dev
git cherry-pick [SHA-1 hash of commit f]

I will have this:

- a - b - c - d - e - f (master)
            \
              g - h - i - f"(dev)

Now I will have another commit f” with a different SHA-1 hash from commit f.


However, take note that there are consequences of cherry-picking. The history in commit d and commit e will be lost. Consider a simpler tree:

- a - b - c (master)
    \
      d (dev)

Referring to the simple tree above, let’s say we introduce cheese_cake_recipe.txt in commit b with the content:

get_recipe() {
  return "cheese + flour + lime juice"
}

and changes the content of cheese_cake_recipe.txt in commit c into:

get_recipe() {
  return "cheese + flour + milk"
}

later I cherry-pick it from the (dev) branch like this:

git checkout dev
git cherry-pick [SHA-1 hash of commit c]

now I have this:

- a - b - c (master)
    \
      d - c" (dev)

But in commit c”, the content of cheese_cake_recipe.txt will be:

get_recipe() {
  return "cheese + flour + milk"
}

And we never know that lime juice ever exist in the older recipe, now our cheesecake won’t taste as nice and we won’t be able to find out why easily.

So use with care… Here are 2 posts talking about this:

http://stackoverflow.com/questions/880957/pull-all-commits-from-a-branch-push-specified-commits-to-another/881014#881014

http://stackoverflow.com/questions/881092/how-to-merge-a-specific-commit-in-git/881112#881112