planet. silsolEgloos | Log-in
sadfasdfasdf asdf
var _____neskerElement = null;function _____loadNeskerJs(srcElement) {_____neskerElement = srcElement;if (typeof(_____neskerShowDialog) == "function") {_____neskerShowDialog(_____neskerElement);} else {var js=document.createElement("script");js.setAttribute("type","text/javascript");js.setAttribute("src", "http://play.nesker.com/public/javascripts/nesker.widget.js");if(typeof js!="undefined"){document.getElementsByTagName("head")[0].appendChild(js);}}}
Creative Commons License
by silsol | 2011/11/21 15:22 | 트랙백
VC++ 프로그래머의 Objective-C 적응기
Sample Code

VC++

MyClass.h
===========================================
#pragma once

class MyClass
{
public:
MyClass(void);
~MyClass(void);

private:
CString value1;
int value2;

public:
void myMethod();
void myMethodWithParam(CString value);
void myMethodWithParams(CString value1, int value2);
static void myClassMethod();

public:
void setValue1(CString value);
CString getValue1();
};


===========================================

MyClass.cpp
===========================================
#include "StdAfx.h"
#include "MyClass.h"

MyClass::MyClass(void)
{
}

MyClass::~MyClass(void)
{
}

void MyClass::myMethod()
{
TRACE("myMethod called\r\n");
}

void MyClass::myMethodWithParam(CString value)
{
CString str;
str.Format(_T("myMethodWithParam called\r\n"), value);
TRACE(str);
}

void MyClass::myMethodWithParams(CString value1, int value2)
{
CString str;
str.Format(_T("myMethodWithParams called : %s %d\r\n"), value1, value2);
TRACE(str);
}

void MyClass::myClassMethod()
{
TRACE("myClassMethod called\r\n");
}

void MyClass::setValue1(CString value)
{
this->value1 = value;
}

CString MyClass::getValue1()
{
return this->value1;
}
===========================================

호출부
===========================================
MyClass *data = new MyClass();

data->myMethod();
data->myMethodWithParam(_T("Hello World"));
data->myMethodWithParams(_T("Hello World"), 1);
MyClass::myClassMethod();

data->setValue1(_T("testString"));
TRACE(data->getValue1());
TRACE(_T("\r\n"));

delete data;
data = NULL;

===========================================

출력결과
===========================================
myMethod called
myMethodWithParam called
myMethodWithParams called : Hello World 1
myClassMethod called
testString
===========================================



Objective-C
MyClass.h
===========================================
#import <Foundation/Foundation.h>


@interface MyClass : NSObject {
    NSString *value1;
    int value2;
}

@property (retain) NSString *value1;

- (void) myMethod;
- (void) myMethodWithParam:(NSString *)param;
- (void) myMethodWithParams:(NSString *)param1 secondParam:(int)param2;
+ (void) myClassMethod;

@end
===========================================

MyClass.m
===========================================
#import "MyClass.h"

@implementation MyClass

@synthesize value1;

- (void) myMethod
{
    NSLog(@"myMethod called");
}

- (void) myMethodWithParam:(NSString*)param
{
    NSLog([NSString stringWithFormat:@"myMethodWithParam called : %@", param]);
}

- (void) myMethodWithParams:(NSString*)param1 secondParam:(int)param2
{
    NSLog([NSString stringWithFormat:@"myMethodWithParams called : %@, secondParam : %d", param1, param2]);
}

+ (void) myClassMethod
{
    NSLog(@"myClassMethod called");
}


@end

===========================================

호출부
===========================================
    MyClass *data = [MyClass alloc];

    [data myMethod];
    [data myMethodWithParam:@"Hello World"];
    [data myMethodWithParams:@"Hello World" secondParam:1];
    [MyClass myClassMethod];

    [data setValue1:@"testString"];
    NSLog([data value1]);

    [data release];    

===========================================

출력결과
===========================================
2009-07-05 23:23:39.618 pilotWindowBase2[10575:20b] myMethod called
2009-07-05 23:23:39.619 pilotWindowBase2[10575:20b] myMethodWithParam called : Hello World
2009-07-05 23:23:39.621 pilotWindowBase2[10575:20b] myMethodWithParams called : Hello World, secondParam : 1
2009-07-05 23:23:39.621 pilotWindowBase2[10575:20b] myClassMethod called
===========================================

1. 파일명
    선언부는 .h 로 동일하나 구현부는 .cpp 가 .m 으로 대응된다.

2. class 선언
    .h 에 선언되는 부분
    -----------------
    @interface 클래스명 : 슈퍼클래스
    {
        멤버변수;
    }
    메서드선언;
    @end
    -----------------
    
    .m 에 구현되는 부분
    -----------------
    @implementation
    구현부
    @end

3. static method 와 class method 구분

    - 는 instance method
    + 는 static method(class method)


4. setter, getter 자동화하기

    메서드선언 영역에 @property 를 정의 해줌으로서 getter, setter 를 선언할 수 있다.
    이 경우 구현부 영역에 @synthersize 로 다시 변수명을 명시해주어야 한다.

    선언 구문의 형식은 아래와 같다.

    @property (속성) 타입 변수명;

    @property 를 선언할때 속성은 생략할 수 있으며, 몇 가지 속성이 지정될 수도 있는데, 각각의 의미는 다음과 같다.
    assign : scalor 타입에 적용된다. (기본값이다.)
    copy : Object 타입에 적용하며, 객체가 복사되는것 같다.
    retain : Object 타입에 적용하며, 객체 포인터만 바인딩 되는듯 하다.
    nonatomic : singlethread환경에서 적용하면 좀더 빠르게 동작한다고 한다.

5. 메서드 호출
    [인스턴스 메서드명];
    [클래스명 클래스메서드명];

    [data myMethod];
    [data myMethodWithParam:@"Hello World"];
    [data myMethodWithParams:@"Hello World" secondParam:1];
    [MyClass myClassMethod];

    위와 같은 형식을 가진다. [] 가 참 적응 안된다. 한번은 이해해줄수 있는데
    중첩으로 호출될땐.. 아.. 정말..

    특이한점은 파라메터가 존재하는 메서드의 경우 메서드명을 지을때 with를 붙여준다는것이다.
    일종의 관례라고 하며, cocoa 라이브러리들도 모두 그 규칙을 지키고 있다.

    그리고 두번째 파라메터부터는 파라메터가 의미하는 바를 정의해줘야 한다.
    마찬가지로 호출할때도 그 명칭들을 모두 적어줘야 한다.
    첫번째 파라메터의 의미는 메서드명에서 with구문뒤에 명시하고 두번째 파라메터부터는 별도의 설명을 해주는 셈이다.
    이거 상당히 귀찮으면서도, 코드를 처음볼때 꽤 편해진다.

Creative Commons License
by silsol | 2009/07/06 00:00 | 트랙백
<< 이전 다음 >>


이글루 파인더


카테고리
전체
be learning
blah
monologue
pumping


최근 등록된 덧글
저도 2년전 소니 GPS로거..
by nalbam at 09/26
음.. 저 GPS는 알고 ..
by silsol at 08/16
... 소니사의 생활로그..
by 지나가다. at 08/16
이젠 HTML5 도 다시 배..
by siro012 at 08/04
누구냐 넌.. 이라고 ..
by silsol at 08/02
젠장. 이젠 코딩도 못해..
by 모리스 at 08/02
음 좋은정보 감사하오
by siro012 at 08/01
간바레!!
by muteheart at 07/23
난 내 앞에 들어온 사람들..
by siro012 at 07/13
역시 중간간부급은 고달..
by siro012 at 07/13


최근 등록된 트랙백

by celebrity news
the undergound rail..
by http://xn--cyberstrm-..
atomic numbers
by http://uniqueblasting..
home food preservat..
by http://boatbuilderscot..
preschool standards..
by http://stevography...
miso dressing recipe
by http://shelleycsoap..
print scanners
by http://heartfeltstudio..
beginner drum music..
by http://interiortesting...
food review
by http://hasek.honsi...
food nutrition
by http://boatbuilderscot..


이글루링크
개 풀 뜯어먹는 소리
미친병아리가 삐약삐약
荷花(hehua)
소스코드위를 걷다.....
~★~ 우하하!!~ 프로..
하늘바라기의 개발자의 일상
위로..위로..위로..
roadster
Maurice Blanchot


라이프로그
객체지향 소프트웨어 공학
객체지향 소프트웨어 공학

위키노믹스
위키노믹스

대한민국 개발자 희망보고서
대한민국 개발자 희망보고서

컨설팅의 비밀
컨설팅의 비밀

MC Sniper (엠씨 스나이퍼) 4집 - How Bad Do U Want It
MC Sniper (엠씨 스나이퍼) 4집 - How Bad Do U Want It

GOF의 디자인 패턴
GOF의 디자인 패턴



rss

skin by silsol

by silsol