[ios] delegate must live longer than owner

http://stackoverflow.com/questions/5091693/check-if-delegate-still-exists-before-calling-respondstoselector

To avoid delegate crash delegate must live longer than owner

Or owner’s delegate property must be weak reference

Sample Code :

https://github.com/sungkipyung/CodeSample/blob/master/DelegateCrashTryCatchExample/DelegateCrashTryCatchExample/ViewController.m


#import "ViewController.h"
#import "SimpleObject.h"

@interface ViewController ()

@property (nonatomic, strong) SimpleObject *obj;
@property (nonatomic, assign) iddelegate;
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.obj = [[SimpleObject alloc] init];
self.delegate = self.obj;
NSLog(@"self.delegate address : %ud", self.delegate);
}

//overcome crash
- (void)setObj:(SimpleObject *)obj
{
_obj = obj;
self.delegate = obj;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)actionDelegateCall:(id)sender {
@try {
NSLog(@"self.delegate address : %ud", self.delegate);
[self.delegate helloWorld]; // what the hell !!!
// delegate must live longer than me!
}
@catch (NSException *exception) {
NSLog(@"%@", exception);
}
@finally {
NSLog(@"Overcome Exception");
}

}
- (IBAction)actionDeallocObj:(id)sender {
NSLog(@"self.delegate address : %ud", self.obj);
NSLog(@"self.delegate address : %ud", self.delegate);

self.obj = nil;
NSLog(@"self.delegate address : %ud", self.obj);
NSLog(@"self.delegate address : %ud", self.delegate);
}

@end

[ios] NSObject respondsToSelector

http://stackoverflow.com/questions/3697058/when-to-use-respondstoselector-in-objective-c

1. 오래된 프레임워크 호환성을 위해 사용

if ([someObject responsToselector:@selector(newApiMethod)]) {
// ios 8
[someObject newAPiMethod];
} else {
// ios 6
[someObject oldAPiMethod];
}

2. Protocol Delegate의 optional로 지정된 메서드를 호출할 때 사용.

[Web] ImageMagick no decode, no encode

If you see below error message when try to resize some jpg images using ImageMagick

{code}
no decode delegate for this image format
no encode delegate for this image format
{code}

read this document carefully
http://www.imagemagick.org/script/advanced-unix-installation.php#mingw

위 내용을 요약해보면…일단 jpeg library를 설치한뒤에

$ yum install libjpeg libjpeg-devel

이미지매직을 다시 설치 하라는 말이다.

$ ./configure ….
$ make
$ make install

jpeg 이 있는지 권한은 잘 들어가있는지 확인한다.

$ identify -list format

JPEG* JPEG rw- Joint Photographic Experts Group JFIF format (62)
JPG* JPEG rw- Joint Photographic Experts Group JFIF format

rw가 의미하는 것(본문 일부 발췌)
You should see a mode of rw- associated with the JPEG tag. This mode means the image can be read or written and can only support one image per image file.

결론은 삽질하지 말고 문서를 잘 봐야 한다.  ^^