달력

4

« 2024/4 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

'모바일'에 해당되는 글 1

  1. 2009.09.07 iPhone programming part 1
2009. 9. 7. 15:38

iPhone programming part 1 모바일2009. 9. 7. 15:38

원본: http://www.gehacktes.net/2009/01/iphone-programming-part-1/

이 예제에서는 property, 2개 이상의 파라미터를 갖는 메소드 정의/구현 및 사용법에 대해서 알 수 있다.

콘솔 프로젝트 생성하기
  - New Project / Command Line Utility / Foundation Tool

Sources폴더에서 cmd+N 누르고, Objective-C class 선택하고, 이름을 classA로 입력해서 새로운 클래스(classA.h, classA.m) 파일 생성

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

@interface classA : NSObject {
    bool myBoolean;
    int myNumber;
    int myIdentifier;
    NSString * myName;   
}

// manual creating getters and setters for a property
-(int) myNumber;
-(void) setMyNumber:(int)value;

// automatic creating getters and setters for a property
@property bool myBoolean;
@property (readonly) int myIdentifier;
@property (assign) NSString *myName;

// methods and functions
-(void) doStuffWithA:(bool)a B:(int)b C:(NSString *)c;

@end

classA.m
#import "classA.h"

@implementation classA

// automatic generation of getters and setters for properties:
@synthesize myBoolean;
@synthesize myIdentifier;
@synthesize myName;

// manual getter and setter methods:
-(void) setMyNumber:(int)value {
    myNumber = value;
}

-(int) myNumber {
    return myNumber;
    // this is ok. It will return the value. To call the function in ObjC
    // you have to write something like:"return [self myNumber];"
}

-(void) doStuffWithA:(bool)a B:(int)b C:(NSString *)c {
    myBoolean = a;
    myNumber = b;
    myName = c;
    myIdentifier = b; // ok, internal writeable - only external readonly
    // not allowed:  self.myNumber = b;
    // dot notations are only allowed for automatic generated properties.
}

@end

main.m
#import <Foundation/Foundation.h>
#import "classA.h"

int main (int argc, const char * argv[]) {
    classA * myObject = [[classA alloc]init];
    [myObject setMyNumber:1]; // ok
    int a = [myObject myNumber]; // ok
    NSLog([[NSString alloc] initWithFormat:@"a=%d\n", a]);
    // myObject.setNumber(1); // not allowed.
    // int a = myObject.myNumber; // only on automatic generated properties.
    [myObject setMyBoolean:true]; // ok
    myObject.myBoolean = true; // ok
    bool b = [myObject myBoolean]; // ok
    b = myObject.myBoolean; // ok
    // [myObject setMyIdentifier:1]; // not allowed : readonly
    // myObject.myIdentifier = 1; // not allowed : readonly
    int i = [myObject myIdentifier]; // ok
    i = myObject.myIdentifier; // ok
    [myObject doStuffWithA:true B:42 C:@"Text"];   
    return 0;
}

:
Posted by codetemplate