SlideShare ist ein Scribd-Unternehmen logo
1 von 19
Objective-C




                  프로퍼티(Property)
                  By Changhoon Park
    Objective-C
                  http://www.wawworld.me
                                           Last Update : 2011. 08. 28

11년 11월 9일 수요일
새 컴파일러 지시자
                          @property                     접근자 문법
                         @synthesize




                                       프로퍼티(Property)



             2


                  프로퍼티     프로퍼티 값 축소하기
    Objective-C            객체 프로퍼티 사용하기

                                                                 By Changhoon Park
                                                                    http://www.wawworld.me



11년 11월 9일 수요일
인터페이스 축소하기


       #import <Foundation/Foundation.h>
       #import "Tire.h"                                  #import <Foundation/Foundation.h>
                                                         #import "Tire.h"
       @interface AllWeatherRadial : Tire {
         float rainHandling;                             @interface AllWeatherRadial : Tire {
         float snowHandling;                               float rainHandling;
       }                                                   float snowHandling;
                                                         }
       - (void) setRainHandling: (float) rainHandling;
       - (float) rainHandling;                           @property float rainHandling;
                                                         @property float snowHandling;
       - (void) setSnowHandling: (float) snowHandling;
       - (float) snowHandling;                           @end // AllWeatherRadial

       @end // AllWeatherRadial




             3


                     프로퍼티       프로퍼티 값 축소하기
    Objective-C                 객체 프로퍼티 사용하기

                                                                                           By Changhoon Park
                                                                                                http://www.wawworld.me



11년 11월 9일 수요일
@property float rainHandling;




    @: 오브젝티브C 만의 운법이 나타냄
    AlIWeatherRadial 클래스의 객체가 float 타입의 애트리뷰트 rainHandling을 가지고 있음을 나타냄

    @property는 애트리뷰트의 세터(setter)와 게터(getter) 메소드를 자동 생성
    - setRainHandling을 호출하여 프로퍼티의 값을 설정 가능
    - rainHandling으로 애트리뷰트에 접근할 수 있음




             4


                  프로퍼티   프로퍼티 값 축소하기
    Objective-C          객체 프로퍼티 사용하기

                                                             By Changhoon Park
                                                                http://www.wawworld.me



11년 11월 9일 수요일
구현 축소하기

       #import "AllWeatherRadial.h"

       @implementation AllWeatherRadial                            #import "AllWeatherRadial.h"
       - (id) initWithPressure: (float) p                          @implementation AllWeatherRadial
                  treadDepth: (float) td
       {                                                           @synthesize rainHandling;
           if (self = [super initWithPressure: p                   @synthesize snowHandling;
                         treadDepth: td]) {
               rainHandling = 23.7;                                - (id) initWithPressure: (float) p
               snowHandling = 42.5;                                           treadDepth: (float) td
           }                                                       {
                                                                       if (self = [super initWithPressure: p
         return (self);                                                              treadDepth: td]) {
                                                                           rainHandling = 23.7;
       } // initWithPressure:treadDepth                                    snowHandling = 42.5;
                                                                       }
       - (void) setRainHandling: (float) rh                          return (self);
       {
           rainHandling = rh;                                      } // initWithPressure:treadDepth
       } // setRainHandling

                                                                   - (NSString *) description
       - (float) rainHandling                                      {
       {                                                              NSString *desc;
           return (rainHandling);                                     desc = [[NSString alloc] initWithFormat:
       } // rainHandling                                                        @"AllWeatherRadial: %.1f / %.1f / %.1f / %.1f",
                                                                             [self pressure], [self treadDepth],
                                                                             [self rainHandling],
       - (void) setSnowHandling: (float) sh                                  [self snowHandling]];
       {
           snowHandling = sh;                                        return (desc);
       } // setSnowHandling
                                                                   } // description
       - (float) snowHandling                                      @end // AllWeatherRadial
       {
           return (snowHandling);
       } // snowHandling




                          5


                                            프로퍼티   프로퍼티 값 축소하기
    Objective-C                                    객체 프로퍼티 사용하기

                                                                                                                                  By Changhoon Park
                                                                                                                                     http://www.wawworld.me



11년 11월 9일 수요일
구현 축소하기


    - (void) setRainHandling: (float) rh {
        rainHandling = rh;
    } // setRainHandling


    - (float) rainHandling {
        return (rainHandling);
    } // rainHandling
                                                 @synthesize rainHandling;
                                                 @synthesize snowHandling;
    - (void) setSnowHandling: (float) sh {
        snowHandling = sh;
    } // setSnowHandling


    - (float) snowHandling {
        return (snowHandling);
    } // snowHandling




             6


                  프로퍼티      프로퍼티 값 축소하기
    Objective-C             객체 프로퍼티 사용하기

                                                                             By Changhoon Park
                                                                                http://www.wawworld.me



11년 11월 9일 수요일
@synthesize rainHandling;




                   「이 메소드의 접근자 메소드를 생성히라」컴파일러 기능
                   -setRainHandling:과 - rainHandling의 컴파일된 코드를 작성




             7


                  프로퍼티   프로퍼티 값 축소하기
    Objective-C          객체 프로퍼티 사용하기

                                                                    By Changhoon Park
                                                                       http://www.wawworld.me



11년 11월 9일 수요일
놀라운 점(.)들


                         오브젝티브 C 2.0 프로퍼티는 객체 애트리뷰트를
                         더 쉽게 접근할 수 있도록 하는 또 다른 문법 제공



                            [tire setRainHandling: 20 + i];
                            [tire setSnowHandling: 28 + i];




                            tire.rainHandling = 20 + i;
                            tire.snowHandling = 28 + ii




             8


                  프로퍼티    프로퍼티 값 축소하기
    Objective-C           객체 프로퍼티 사용하기

                                                              By Changhoon Park
                                                                 http://www.wawworld.me



11년 11월 9일 수요일
놀라운 점(.)들




                              점 (.)을 대입 부호 죄측에서 사용:
                          애트리뷰트 이름에 해당하는 세터 메소드
                         (-setRainHandling, -setSnowHanling) 호출


                                   우측에 점 연산자 사용 :
                           해당 애트리뷰트 이름의 게터 메소드
                           (­ rainHandling. -snowHanding ) 호출




             9


                  프로퍼티   프로퍼티 값 축소하기
    Objective-C          객체 프로퍼티 사용하기

                                                                  By Changhoon Park
                                                                     http://www.wawworld.me



11년 11월 9일 수요일
interface

      #import <Cocoa/Cocoa.h>                                        #import <Cocoa/Cocoa.h>

      @class Tire;                                                   @class Tire;
      @class Engine;                                                 @class Engine;

      @interface Car : NSObject {                                    @interface Car : NSObject {
        NSMutableArray *tires;                                         NSString *name;
        Engine *engine;                                                NSMutableArray *tires;
      }                                                                Engine *engine;
                                                                     }
      - (void) setEngine: (Engine *) newEngine;
      - (Engine *) engine;                                           @property (readwrite, copy) NSString *name;
                                                                     @property (readwrite, retain) Engine *engine;

      - (void) setTire: (Tire *) tire atIndex: (int) index;          - (void) setTire: (Tire *) tire atIndex: (int) index;
      - (Tire *) tireAtIndex: (int) index;                           - (Tire *) tireAtIndex: (int) index;
      - (void) print;                                                - (void) print;

      @end // Car                                                    @end // Car




             10


                        프로퍼티           프로퍼티 값 축소하기
    Objective-C                        객체 프로퍼티 사용하기

                                                                                                                     By Changhoon Park
                                                                                                                             http://www.wawworld.me



11년 11월 9일 수요일
interface



                            @property (copy) NSString *name;
                            @property (retain) Engine *engine;




                  • name : copy를 추가해 주면 name이 북시될 것
                  • engine: retain 참조/릴리즈로 관리
                  • copy나 retain을 지정해 주지 않으연 컴파일러는 지동으로
                   기본 값인 assign을 할당하는데 객체에서 사용하기에는적합하지
                   않다




            11


                   프로퍼티   프로퍼티 값 축소하기
    Objective-C           객체 프로퍼티 사용하기

                                                                 By Changhoon Park
                                                                    http://www.wawworld.me



11년 11월 9일 수요일
implementation




                    @implementation Car

                    @synthesize name;
                    @synthesize engine;




            12


                  프로퍼티   프로퍼티 값 축소하기
    Objective-C          객체 프로퍼티 사용하기

                                                     By Changhoon Park
                                                        http://www.wawworld.me



11년 11월 9일 수요일
main




                    Engine *engine = [[Slant6 alloc] init];
                    [car setEngine: engine];




                    Slant6 *engine = [[Slant6 alloc] init];
                    car.engine = engine;




            13


                  프로퍼티   프로퍼티 값 축소하기
    Objective-C          객체 프로퍼티 사용하기

                                                              By Changhoon Park
                                                                 http://www.wawworld.me



11년 11월 9일 수요일
이름 바꾸기


                    @interface Car : NSObject {
                        NSString *application;
                        NSMutableArray *tires;
                        Engine *engine;
                    }

                    @property (copy) NSString *name;
                    @property (retain) Engine *engine;



                    @synthesize name = appellation;




            14


                  프로퍼티   프로퍼티 값 축소하기
    Objective-C          객체 프로퍼티 사용하기

                                                         By Changhoon Park
                                                            http://www.wawworld.me



11년 11월 9일 수요일
이름 바꾸기


                    @interface Car : NSObject {
                        NSString *application;
                        NSMutableArray *tires;
                        Engine *engine;
                    }

                    @property (copy) NSString *name;
                    @property (retain) Engine *engine;



                    @synthesize name = appellation;




            15


                  프로퍼티   프로퍼티 값 축소하기
    Objective-C          객체 프로퍼티 사용하기

                                                         By Changhoon Park
                                                            http://www.wawworld.me



11년 11월 9일 수요일
이름 바꾸기




                     @synthesize name = appellation;




                  • 컴파일러는 여전히 -setName 과 -name을 생성
                  • 메소드 구현 내부에서는 appellation 인스턴스 변수 사용




            16


                   프로퍼티   프로퍼티 값 축소하기
    Objective-C           객체 프로퍼티 사용하기

                                                          By Changhoon Park
                                                             http://www.wawworld.me



11년 11월 9일 수요일
이름 바꾸기




                 name = @"Car";                self.name = @"Car";




                  • 이제 name을 검색하여 대치하거나
                  • 인스턴스 변수에 직접 접근하는 코드 대신 프로퍼티 사용




            17


                    프로퍼티   프로퍼티 값 축소하기
    Objective-C            객체 프로퍼티 사용하기

                                                                     By Changhoon Park
                                                                        http://www.wawworld.me



11년 11월 9일 수요일
읽기 전용으로 설정


                 @property (readwrite, copy) NSString *name;
                 @property! (readwrite ,retain)! Engιne! *engine;



                 @interface Me : NSObject {
                 ! float!shoesSize;
                 ! NSString *licenseNumber;
                 }

                 @property (readwrite) float shoesSize;
                 @property (readonly) NSString * licenseNumber;

                 @end



            18


                     프로퍼티   프로퍼티 값 축소하기
    Objective-C             객체 프로퍼티 사용하기

                                                                    By Changhoon Park
                                                                       http://www.wawworld.me



11년 11월 9일 수요일
프로퍼티도 만능은 아니다




                     - (void) setTire: (Tire *) tire
                              atIndex: (int) index;
                     - (Tire *) tireAtIndex: (int) index;




                  • 자동차에서 타이어의 위치와 같이 추가적인 인수를
                   받는 메소드는 대치 할 수없다




            19


                   프로퍼티   프로퍼티 값 축소하기
    Objective-C           객체 프로퍼티 사용하기

                                                            By Changhoon Park
                                                               http://www.wawworld.me



11년 11월 9일 수요일

Weitere ähnliche Inhalte

Mehr von Hoseo University (12)

Game ai.fsm.02
Game ai.fsm.02Game ai.fsm.02
Game ai.fsm.02
 
Game ai.fsm.01
Game ai.fsm.01Game ai.fsm.01
Game ai.fsm.01
 
Game math.points and lines
Game math.points and linesGame math.points and lines
Game math.points and lines
 
Esl podcast 743 – writing a story
Esl podcast 743 – writing a storyEsl podcast 743 – writing a story
Esl podcast 743 – writing a story
 
목적이 부여된 에이전트 행동
목적이 부여된 에이전트 행동목적이 부여된 에이전트 행동
목적이 부여된 에이전트 행동
 
FoundationKit
FoundationKitFoundationKit
FoundationKit
 
Raven
RavenRaven
Raven
 
프로젝트 구성
프로젝트 구성프로젝트 구성
프로젝트 구성
 
구성(Composition)
구성(Composition)구성(Composition)
구성(Composition)
 
Objective-C에서의 OOP
Objective-C에서의 OOPObjective-C에서의 OOP
Objective-C에서의 OOP
 
Dt2210.01.syllabus
Dt2210.01.syllabusDt2210.01.syllabus
Dt2210.01.syllabus
 
Dt3160.01
Dt3160.01Dt3160.01
Dt3160.01
 

Property

  • 1. Objective-C 프로퍼티(Property) By Changhoon Park Objective-C http://www.wawworld.me Last Update : 2011. 08. 28 11년 11월 9일 수요일
  • 2. 새 컴파일러 지시자 @property 접근자 문법 @synthesize 프로퍼티(Property) 2 프로퍼티 프로퍼티 값 축소하기 Objective-C 객체 프로퍼티 사용하기 By Changhoon Park http://www.wawworld.me 11년 11월 9일 수요일
  • 3. 인터페이스 축소하기 #import <Foundation/Foundation.h> #import "Tire.h" #import <Foundation/Foundation.h> #import "Tire.h" @interface AllWeatherRadial : Tire { float rainHandling; @interface AllWeatherRadial : Tire { float snowHandling; float rainHandling; } float snowHandling; } - (void) setRainHandling: (float) rainHandling; - (float) rainHandling; @property float rainHandling; @property float snowHandling; - (void) setSnowHandling: (float) snowHandling; - (float) snowHandling; @end // AllWeatherRadial @end // AllWeatherRadial 3 프로퍼티 프로퍼티 값 축소하기 Objective-C 객체 프로퍼티 사용하기 By Changhoon Park http://www.wawworld.me 11년 11월 9일 수요일
  • 4. @property float rainHandling; @: 오브젝티브C 만의 운법이 나타냄 AlIWeatherRadial 클래스의 객체가 float 타입의 애트리뷰트 rainHandling을 가지고 있음을 나타냄 @property는 애트리뷰트의 세터(setter)와 게터(getter) 메소드를 자동 생성 - setRainHandling을 호출하여 프로퍼티의 값을 설정 가능 - rainHandling으로 애트리뷰트에 접근할 수 있음 4 프로퍼티 프로퍼티 값 축소하기 Objective-C 객체 프로퍼티 사용하기 By Changhoon Park http://www.wawworld.me 11년 11월 9일 수요일
  • 5. 구현 축소하기 #import "AllWeatherRadial.h" @implementation AllWeatherRadial #import "AllWeatherRadial.h" - (id) initWithPressure: (float) p @implementation AllWeatherRadial treadDepth: (float) td { @synthesize rainHandling; if (self = [super initWithPressure: p @synthesize snowHandling; treadDepth: td]) { rainHandling = 23.7; - (id) initWithPressure: (float) p snowHandling = 42.5; treadDepth: (float) td } { if (self = [super initWithPressure: p return (self); treadDepth: td]) { rainHandling = 23.7; } // initWithPressure:treadDepth snowHandling = 42.5; } - (void) setRainHandling: (float) rh return (self); { rainHandling = rh; } // initWithPressure:treadDepth } // setRainHandling - (NSString *) description - (float) rainHandling { { NSString *desc; return (rainHandling); desc = [[NSString alloc] initWithFormat: } // rainHandling @"AllWeatherRadial: %.1f / %.1f / %.1f / %.1f", [self pressure], [self treadDepth], [self rainHandling], - (void) setSnowHandling: (float) sh [self snowHandling]]; { snowHandling = sh; return (desc); } // setSnowHandling } // description - (float) snowHandling @end // AllWeatherRadial { return (snowHandling); } // snowHandling 5 프로퍼티 프로퍼티 값 축소하기 Objective-C 객체 프로퍼티 사용하기 By Changhoon Park http://www.wawworld.me 11년 11월 9일 수요일
  • 6. 구현 축소하기 - (void) setRainHandling: (float) rh { rainHandling = rh; } // setRainHandling - (float) rainHandling { return (rainHandling); } // rainHandling @synthesize rainHandling; @synthesize snowHandling; - (void) setSnowHandling: (float) sh { snowHandling = sh; } // setSnowHandling - (float) snowHandling { return (snowHandling); } // snowHandling 6 프로퍼티 프로퍼티 값 축소하기 Objective-C 객체 프로퍼티 사용하기 By Changhoon Park http://www.wawworld.me 11년 11월 9일 수요일
  • 7. @synthesize rainHandling; 「이 메소드의 접근자 메소드를 생성히라」컴파일러 기능 -setRainHandling:과 - rainHandling의 컴파일된 코드를 작성 7 프로퍼티 프로퍼티 값 축소하기 Objective-C 객체 프로퍼티 사용하기 By Changhoon Park http://www.wawworld.me 11년 11월 9일 수요일
  • 8. 놀라운 점(.)들 오브젝티브 C 2.0 프로퍼티는 객체 애트리뷰트를 더 쉽게 접근할 수 있도록 하는 또 다른 문법 제공 [tire setRainHandling: 20 + i]; [tire setSnowHandling: 28 + i]; tire.rainHandling = 20 + i; tire.snowHandling = 28 + ii 8 프로퍼티 프로퍼티 값 축소하기 Objective-C 객체 프로퍼티 사용하기 By Changhoon Park http://www.wawworld.me 11년 11월 9일 수요일
  • 9. 놀라운 점(.)들 점 (.)을 대입 부호 죄측에서 사용: 애트리뷰트 이름에 해당하는 세터 메소드 (-setRainHandling, -setSnowHanling) 호출 우측에 점 연산자 사용 : 해당 애트리뷰트 이름의 게터 메소드 (­ rainHandling. -snowHanding ) 호출 9 프로퍼티 프로퍼티 값 축소하기 Objective-C 객체 프로퍼티 사용하기 By Changhoon Park http://www.wawworld.me 11년 11월 9일 수요일
  • 10. interface #import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h> @class Tire; @class Tire; @class Engine; @class Engine; @interface Car : NSObject { @interface Car : NSObject { NSMutableArray *tires; NSString *name; Engine *engine; NSMutableArray *tires; } Engine *engine; } - (void) setEngine: (Engine *) newEngine; - (Engine *) engine; @property (readwrite, copy) NSString *name; @property (readwrite, retain) Engine *engine; - (void) setTire: (Tire *) tire atIndex: (int) index; - (void) setTire: (Tire *) tire atIndex: (int) index; - (Tire *) tireAtIndex: (int) index; - (Tire *) tireAtIndex: (int) index; - (void) print; - (void) print; @end // Car @end // Car 10 프로퍼티 프로퍼티 값 축소하기 Objective-C 객체 프로퍼티 사용하기 By Changhoon Park http://www.wawworld.me 11년 11월 9일 수요일
  • 11. interface @property (copy) NSString *name; @property (retain) Engine *engine; • name : copy를 추가해 주면 name이 북시될 것 • engine: retain 참조/릴리즈로 관리 • copy나 retain을 지정해 주지 않으연 컴파일러는 지동으로 기본 값인 assign을 할당하는데 객체에서 사용하기에는적합하지 않다 11 프로퍼티 프로퍼티 값 축소하기 Objective-C 객체 프로퍼티 사용하기 By Changhoon Park http://www.wawworld.me 11년 11월 9일 수요일
  • 12. implementation @implementation Car @synthesize name; @synthesize engine; 12 프로퍼티 프로퍼티 값 축소하기 Objective-C 객체 프로퍼티 사용하기 By Changhoon Park http://www.wawworld.me 11년 11월 9일 수요일
  • 13. main Engine *engine = [[Slant6 alloc] init]; [car setEngine: engine]; Slant6 *engine = [[Slant6 alloc] init]; car.engine = engine; 13 프로퍼티 프로퍼티 값 축소하기 Objective-C 객체 프로퍼티 사용하기 By Changhoon Park http://www.wawworld.me 11년 11월 9일 수요일
  • 14. 이름 바꾸기 @interface Car : NSObject { NSString *application; NSMutableArray *tires; Engine *engine; } @property (copy) NSString *name; @property (retain) Engine *engine; @synthesize name = appellation; 14 프로퍼티 프로퍼티 값 축소하기 Objective-C 객체 프로퍼티 사용하기 By Changhoon Park http://www.wawworld.me 11년 11월 9일 수요일
  • 15. 이름 바꾸기 @interface Car : NSObject { NSString *application; NSMutableArray *tires; Engine *engine; } @property (copy) NSString *name; @property (retain) Engine *engine; @synthesize name = appellation; 15 프로퍼티 프로퍼티 값 축소하기 Objective-C 객체 프로퍼티 사용하기 By Changhoon Park http://www.wawworld.me 11년 11월 9일 수요일
  • 16. 이름 바꾸기 @synthesize name = appellation; • 컴파일러는 여전히 -setName 과 -name을 생성 • 메소드 구현 내부에서는 appellation 인스턴스 변수 사용 16 프로퍼티 프로퍼티 값 축소하기 Objective-C 객체 프로퍼티 사용하기 By Changhoon Park http://www.wawworld.me 11년 11월 9일 수요일
  • 17. 이름 바꾸기 name = @"Car"; self.name = @"Car"; • 이제 name을 검색하여 대치하거나 • 인스턴스 변수에 직접 접근하는 코드 대신 프로퍼티 사용 17 프로퍼티 프로퍼티 값 축소하기 Objective-C 객체 프로퍼티 사용하기 By Changhoon Park http://www.wawworld.me 11년 11월 9일 수요일
  • 18. 읽기 전용으로 설정 @property (readwrite, copy) NSString *name; @property! (readwrite ,retain)! Engιne! *engine; @interface Me : NSObject { ! float!shoesSize; ! NSString *licenseNumber; } @property (readwrite) float shoesSize; @property (readonly) NSString * licenseNumber; @end 18 프로퍼티 프로퍼티 값 축소하기 Objective-C 객체 프로퍼티 사용하기 By Changhoon Park http://www.wawworld.me 11년 11월 9일 수요일
  • 19. 프로퍼티도 만능은 아니다 - (void) setTire: (Tire *) tire atIndex: (int) index; - (Tire *) tireAtIndex: (int) index; • 자동차에서 타이어의 위치와 같이 추가적인 인수를 받는 메소드는 대치 할 수없다 19 프로퍼티 프로퍼티 값 축소하기 Objective-C 객체 프로퍼티 사용하기 By Changhoon Park http://www.wawworld.me 11년 11월 9일 수요일