Wednesday, March 21, 2012

pythonbrew installation memo

pythonbrew installation memo

GitHab - utahta/pythonbrew

* install via easy install
$ easy_install pythonbrew
...
$ pythonbrew_install


* add the following line to ./bash_profile
source [YOUR_HOME]/.pythonbrew/etc/bashrc


* install the different version of python from your os built in.
$ pythonbrew install --no-test 3.2.1
note: set --no-test option. Because the following error occurred:
unclosed file <_io.TextIOWrapper name='/dev/null' mode='a' encoding='UTF-8'>
refer to:
A Way of Code - Mac OS X LionでPython 2.7.2をビルドするとエラーになる


* check the installed version of python
$ pybrew list
# pythonbrew pythons
Python-3.2.1


* switch the version 3.2.1
$ pybrew switch 3.2.1
Switched to Python-3.2.1


* create virtualenv for the version.
$ pybrew venv create py32 -p 3.2.1


* check the environment
$ pybrew venv list
# virtualenv for Python-3.2.1 (found in [YOUR_HOME]/.pythonbrew/venvs/Python-3.2.1)
py32


* use the virtual environment
$ pybrew venv use py32


* deactivate the virtual environment
$ deactivate


* off the pythonbrew to use os built-in python.
$ pybrew off

Thursday, January 26, 2012

objective-C Slice NSArray

あまり使わないかも知れないけれど


NSArray *array = [NSArray arrayWithObjects:@"Python", @"Objective-C", @"JavaScript", @"Erlang", nil];
NSRange range = NSMakeRange(1, 3);
NSLog(@"objectsAtIndexes(1, 2, 3):%@",[array subarrayWithRange:range]);



NSMakeRangeで開始indexとlengthを指定してNSRangeを作る。とこんな感じ。

> objectsAtIndexes(1, 2, 3):(
"Objective-C",
JavaScript,
Erlang
)

Wednesday, January 25, 2012

NavigationBarの位置がずれ, Status barの裏に描画されてしまう

iPhoneに内蔵されているPhotosアプリのように、画像をフルスクリーンで表示する際
ステータスバーを隠すため、setStatusBarHidden:を呼び出すことがあります。

ステータスバーをhidden=YESにしたとき、ナビゲーションバーの位置はそのままですが、
ここで、Homeボタンを押し、もう一度アプリを起動すると
ナビゲーションバーの位置はステータスバーの無いwindowでの位置に移動してしまいます。

また、フルスクリーンのViewをFadeoutするときに、ステータスバーをhidden=NOにしても
ナビゲーションバーの位置は動かず、ステータスバーの裏に表示されます。

どうやらiosのステータスバーの表示・非常時とナビゲーションバーの位置の移動は
連動しないように設計されているようです。

ですが、繰り返しになりますが、
アプリをバックグラウンドへまわしてもう一度フォアグラウンドへ戻したときは
ナビゲーションバーの位置が新しくセットされてしまうので、

この点は注意しなければいけないようです。





結局ステータスバーをhidden=NOにするときに一度ナビゲーションバーを非表示にし、すぐに表示しなおすことで
位置の修正をしました。




@implementation FirstViewController{
UIView *overView;
}

#pragma mark - View lifecycle
- (void)loadView
{
[super loadView];
UIApplication *app = [UIApplication sharedApplication];

overView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
overView.backgroundColor = [UIColor whiteColor];
overView.alpha = 0.0f;
overView.userInteractionEnabled = NO;
[app.keyWindow addSubview:overView];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UIApplication *app = [UIApplication sharedApplication];

if(app.statusBarHidden == NO){
[app setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
[UIView animateWithDuration:0.4f animations:^(void){
overView.alpha = 1.0f;
}];

} else {
[app setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];

// hiding / showing navigationBar
[self.navigationController setNavigationBarHidden:YES];
[self.navigationController setNavigationBarHidden:NO];

[UIView animateWithDuration:0.4f animations:^(void){
overView.alpha = 0.0f;
}];
}
}

Wednesday, January 18, 2012

Debug Build. Delay of invocation of dealloc method.

With ARC and MRC mixed project, I found an delay of invocation of dealloc method.

When you assign a retained object to weak variable, this object will be released after the assignment immediately.
If you add the variable to a mutable array, it'll crash.

Because that we can not add nil to NSMutableArray.

But this crash happened only when the application was built by Release Build counfiguration.

When the application was built by Debug Build configuration, the crash did not happen.
I think the reason why the difference happened is the delay of invocation of dealloc method.

For example, I assign a retained object to weak variable 'obj'.
And then I add the
variable to a mutable array.
- (void)loadView
{
[super loadView];
__weak MyObject* obj = [MyObject instance];

NSMutableArray *arrayM = [NSMutableArray array];
[arrayM addObject:obj];
NSLog(@"object was inserted to array.");
}



MyObject is a custom object with MRC (-fno-obj-arc flagged).
@implementation MyObject
+ (id)instance
{
return [[[self alloc]init] autorelease];
}

- (void)dealloc
{
NSLog(@"dealloc");
[super dealloc];
}
@end



In the case of Release Build configuration, the dealloc method invoked before add to mutable array.
and it crash.
012-01-18 16:56:15.353 WhatTheNSMutableArray[3096:707] dealloc
2012-01-18 16:56:15.358 WhatTheNSMutableArray[3096:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(0x3717a8bf 0x318481e5 0x370cf20f 0x2651 0x3274d78b 0x3274bf9d 0x3273e941 0x327b0541 0x24d3 0x3274c7eb 0x327463bd 0x32714921 0x327143bf 0x32713d2d 0x31287df3 0x3714e553 0x3714e4f5 0x3714d343 0x370d04dd 0x370d03a5 0x32745457 0x32742743 0x2331 0x22dc)
terminate called throwing an exception(gdb)



But in the case of Debug Build configuration, the dealloc method invoked AFTER add to mutable array.
In spite of wrong code, it does NOT CRASH.
2012-01-18 17:24:36.342 WhatTheNSMutableArray[3175:707] object was inserted to array.
2012-01-18 17:24:36.352 WhatTheNSMutableArray[3175:707] dealloc



I should be careful to this delay. because i can't found the weak assignment mistake.

You can easily find a similar code. like this, but this is wrong.
__weak ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:...

[request setCompletionBlock:^{
[listOfResponseData addObject: request.responseData];
}];


Correctly as follows:
ASIHTTPRequest *_request = [[ASIHTTPRequest alloc] initWithURL:...
__weak ASIHTTPRequest *request = _request;
[request setCompletionBlock:^{
[listOfResponseData addObject: request.responseData];
}];

Tuesday, January 10, 2012

implement Accessor Method, Objective-C

see "Property Declaration Attributes" , Objective-C Programming Language > Declared Properties
http://t.co/JpxDVNHs

example: In this case, a RootViewController has "myStatus" property. I implemented accessor methods of this.
The setter method will set a value to the private instance "_myStatus" and text of "myLabel".
The getter method will return the instance value.


#import
typedef enum {
MyStatusTypeFine,
MyStatusTypePrettyGood
} MyStatusType;

@interface RootViewController : UIViewController

@property (setter = setMyStatus:, getter = getMyStatus) MyStatusType myStatus;
@property (strong) UILabel *myLabel;
@end



@implementation TopViewController{
MyStatusType _myStatus;
}
@synthesize myStatus = _myStatus;
@synthesize myLabel;

- (void)setMyStatus:(MyStatusType)myStatus
{
_myStatus = myStatus;
switch (myStatus) {
case MyStatusTypeFine:
self.myLabel.text = @"Fine.";
[self.myLabel sizeToFit];
self.myLabel.center = self.view.center;
break;

case MyStatusTypePrettyGood:
self.myLabel.text = @"Pretty good.";
[self.myLabel sizeToFit];
self.myLabel.center = self.view.center;
break;

default:
break;
}
}

- (MyStatusType)getMyStatus
{
return _myStatus;
}


#pragma mark - View lifecycle
- (void)loadView
{
[super loadView];

self.myLabel = [UILabel new];
[self.view addSubview: self.myLabel];

// set status
self.myStatus = MyStatusTypeFine;
}