Friday, July 03, 2009

Subclassing an Objective-C class

The previous post describe how to use python class from Objective C. Now, we want to create a python class that subclass from an Objective-C class:

@interface ObjCClass : NSObject {

NSString *aString;

}


@property (retain) NSString *aString;

@end


@implementation ObjCClass

@synthesize aString;


- (id) init

{

self = [super init];

if (self != nil)

{

aString = @"hello from obj-c";

}

return self;

}



@end


We modify the python file by adding a subclass declaration:

from Foundation import *

import objc


ObjCClass = objc.lookUpClass("ObjCClass")

class TestClass (ObjCClass):

def doit_(self, input):

print "sent from obj-C:", input

print "obj-C attribute:", self.aString()

self.setAString_("python modified attribute")

return "hello from Python"


The ObjCClass = objc.lookUpClass("ObjCClass") responsible for finding class in objective C. Note that when we use this, it is impossible to run this Python script alone.

Making objective-C works with Python

Here are simple steps when you want to make Objective-C sees Python class.

1. Prepare your Python file. Here you can use objective-C clases and etc.

from Foundation import *

import objc


class TestClass (NSObject):

def doit_(self, input):

print input

return "hello from Python"

2. Create a plug-in. Create a setup script like this (replace 'helloPython.py' with your script name):

from distutils.core import setup

import py2app

setup(

plugin = ['helloPython.py']

)


And run the setup script with this command:
python setup.py py2app

You'll get a .plugin folder in dist folder

3. Adds plug-in to your project. Drag the file into Resource folder in your Xcode project. Choose: Create Folder References for any added folders when Xcode ask

4. Load plug-in in objective-C by adding this code (change helloPython to your .plugin name)

NSString *pluginPath = [[NSBundle mainBundle]

pathForResource:@"helloPython"

ofType:@"plugin"];

NSBundle *pluginBundle = [NSBundle bundleWithPath:pluginPath];

[pluginBundle load];

4. Creating a python-class object in Objective-C

Class TestClass = NSClassFromString(@"TestClass");

id pyObj = [[TestClass init] alloc];

NSString *result = [pyObj doit:@"sent from obj-c"];

NSLog(result);

Sunday, May 24, 2009

Free Yoga video

It started to rain when I want to run.  But I found a good Yoga video: 
http://video.google.com/videoplay?docid=-4628036076291757842

Thursday, May 21, 2009

For Drupal developer on Mac on TextMate, there is a script that generates drupal function list and put in TextMate module.

see: 
http://acko.net/blog/updated-drupal-textmate-bundle#attachments
I'm putting this blog as a <span style="font-weight: bold;">persistent storage for microblogging</span> now. &nbsp;You can expect to se something short, but frequent.

Saturday, December 13, 2008


You are The Sun


Happiness, Content, Joy.


The meanings for the Sun are fairly simple and consistent.


Young, healthy, new, fresh. The brain is working, things that were muddled come clear, everything falls into place, and everything seems to go your way.


The Sun is ruled by the Sun, of course. This is the light that comes after the long dark night, Apollo to the Moon's Diana. A positive card, it promises you your day in the sun. Glory, gain, triumph, pleasure, truth, success. As the moon symbolized inspiration from the unconscious, from dreams, this card symbolizes discoveries made fully consciousness and wide awake. You have an understanding and enjoyment of science and math, beautifully constructed music, carefully reasoned philosophy. It is a card of intellect, clarity of mind, and feelings of youthful energy.


What Tarot Card are You?
Take the Test to Find Out.


For five or six years ago, I always get the Magician.  Now, it is changing.

(via chakrit's weblog)

Sunday, November 23, 2008

Life is getting more interesting


As the life is getting more interesting, number of windows on your desktop will increases exponentially.

Sunday, November 09, 2008

เอากันไปใหญ่

ภาพจากมติชนออนไลน์

ตกลงกลายเป็นผู้ศักดิ์สิทธิ์ไปแล้วเรอะ?

Saturday, August 30, 2008

Glasgow names

Glasgow
Wikipedia said that there are two possible origins of 'Glasgow'; both are from Gaelic. 'glas cau' = green hollow, and 'Glasgu' = dear green place.

Glaswegian
Glas(gow) + Galwegian, person from Galloway (from Medieval Latin Galwidia, Galloway, a region of southern Scotland).

Fish and ring emblem
Queen deceived the king by giving the marital ring to a soldier she secretly love. The king discovered and get the ring from the soldier and throw it in the river. The king demand the ring from the queen. The queen seek help from St. Kentigern who ordered a soldier to catch the first salmon from the river. Inside lies the ring.
Scotland guide