Category Archives: iPhone Programming

Properties

nonatomic
– By default, access to the property is protected by guards that prevent multiple threads from interfering with each other. Normally this is not a problem, and one would specify nonatomic because it is faster.
-nonatomic will make no protection against multiple threads

retain/copy/assign
– retain increases the retain count and asserts ownership of the object
– copy creates a new variable with a new pointer and a retain count of 1
– assign just assigns, and it ithe default

readwrite/readonly
– readonly is used when only the getter will be declared
– the default is readwrite

// Person.h 헤더 파일
@interface Person : NSObject
{
     NSString * name;
     int age;
     BOOL enable;
     UIImage * image;
}
@property int age;
@property (nonatomic, copy) NSString * name;
@property (readonly, assign) BOOL enable;
@property (nonatomic, retain) UIImage * image;
//  위의 정의(declaration)는 아래의 getter/setter 정의를 생성해준다
// – (int) age;
// – (void) setAge: (int) newValue;
// – (NSString *) name;
// – (void) setName: (NSString *) newValue;
// – (BOOL) enable;
// – (UIImage *) image;
// – (void) setImage: (UIImage *) newValue;
@end

// Person.m 구현 파일
@implementation Person
@synthesize age;
@synthesize name;
@synthesize enable;
//  위의 코드는 아래의 getter/setter 코드를 생성해준다
// – (int) age {
//      return age;
// }
// – (void) setAge: (int) newValue {
//     age = newValue;
// }
// value 객체 (NSString, NSInteger와 같은)가 함수의 인자로 전달되거나
// 함수로부터 반환될 때, 객체자체보다는 일반적으로 복사본(copy)를 사용한다.

// – (NSString *) name {
//     return name;
// }
// – (void) setName: (NSString *) newValue {
//     if (name != newValue) {
//          [name release];
//          name = [newValue copy];
//     }
// }
// – (BOOL) enable {
//      return enable;
// }
// image 에 새로운 객체를 설정하면, 그 새로운 객체에 retain 을 한번 호출해 준다.
// 이는 외부에서 그 객체를 release 하더라도 객체가 메모리 해제되지 않도록
// 유지하기 위함이다.
// – (UIImage *) image {
//     return image;
// }
// – (void) setImage: (UIImage *) newValue {
//     if (image != newValue) {
//          [image release];
//          image = [newValue retain];
//     }
// }

-(id) init {
   // …. 중간생략
}
@end


Collection

Collector 클래스
<variables>
     NSMutableDictionary *counts
     double minimumAllowedNumber
     double maximumAllowedNumber
<methods>
     -collect
<properties>
     totalStringCount
     totalNumberCount
     capitalizedStringCount
     NSSet *strings
     NSSet *numbers
     minimumAllowedNumber
     maximumAllowedNumber

CollectionViewController 클래스
<variables>
     totalStringCountLabel
     totalNumberCountLabel
     capitalizedStringsCountLabel
     uniqueStringsCountLabel
     uniqueNumbersCountLabel
     Collector *model
<methods>
    -collect
<properties>
     totalStringCountLabel
     totalNumberCountLabel
     capitalizedStringsCountLabel
     uniqueStringsCountLabel
     uniqueNumbersCountLabel

CollectionViewController.xib사용자 삽입 이미지

6531386710.zipgrab a source code here.

Fundamental Design Patterns

Fundamental Design Patterns
http://developer.apple.com/library/mac/#documentation/General/Conceptual/
Mac101/Articles/02_DesignPatterns.html#//apple_ref/doc/uid/TP40010611-CH3-SW3


Use of the Model-View-Controller (MVC) design pattern ensures that the objects you create now can be reused or updated easily in future versions of your application. http://developer.apple.com/library/mac/#documentation/General/Conceptual/DevPedia-CocoaCore/MVC.html#//apple_ref/doc/uid/TP40008195-CH32

The delegation design pattern allows you to change the runtime behavior of an object without subclassing. Delegation is a pattern where one object sends messages to another object—specified as its delegate—to ask for input or to notify the it that an event is occurring.
http://developer.apple.com/library/mac/#documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html#//apple_ref/doc/uid/TP40008195-CH14

Controls use the target-action design pattern to notify your application of user interactions. Target-Action is a design pattern in which an object holds the information necessary to send a message to another object when an event occurs.
http://developer.apple.com/library/mac/#documentation/General/Conceptual/Devpedia-CocoaApp/TargetAction.html#//apple_ref/doc/uid/TP40009071-CH3

Other Design Patterns
http://developer.apple.com/library/mac/#documentation/General/Conceptual/
MOSXAppProgrammingGuide/CoreAppDesign/CoreAppDesign.html#//
apple_ref/doc/uid/TP40010543-CH3-SW1



http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/
CocoaFundamentals/CocoaDesignPatterns/CocoaDesignPatterns.html#//
apple_ref/doc/uid/TP40002974-CH6-SW6


Mac OS X Cocoa Application

프로젝트 새로 만들기
-Xcode 4.0.2를 시작하고, 메뉴에서 File>New>Mac OS X Application>Cocoa Application을 선택한 후 “프로젝트 이름 (예를 들어, Person)”를 적고 확인버튼을 누른다.사용자 삽입 이미지사용자 삽입 이미지

코드추가
– 빈 프로젝트 안에 Person 클래스를 새로 추가하고 (Person.h & Person.m 생성) 테스트 (PersonAppDelegate.m 안에 -applicationDidFinishLaunching 메소드 안에서)한다.
사용자 삽입 이미지사용자 삽입 이미지사용자 삽입 이미지사용자 삽입 이미지
빌드와 실행 (cmd+r)
-Output 창에서 출력을 확인한다.사용자 삽입 이미지