Objective-C
|
오브젝티브-C (Objective-C, 종종 ObjC로 표기) 언어는 C 프로그래밍 언어에 스몰토크 스타일의 메시지 구문을 추가한 객체 지향 언어이다.
현재, 이 언어는 애플의 매킨토시의 운영 체제인 맥 오에스 텐과 아이폰의 운영 체제인 iOS에서 사용되고 있다. 오브젝티브-C는 애플의 코코아를 사용하기 위한 기본 언어이며, 원래는 넥스트의 NeXTSTEP 운영 체제에서 주 언어였다. 일반적인(Generic) 오브젝티브-C는 앞에서 언급한 라이브러리를 사용하지 않는다.
Basic
- (펌) Objective C의 기본 문법
- ObjectiveC:DefaultClassTemplate: Objective-C 기본 클래스 작성 방법.
- XCode
- XCode:iPhone:Basic - iPhone 앱 개발 기초.
- iOS
- Core MIDI - Communicate with MIDI devices such as hardware keyboards and synthesizers.
- Core Audio
- Apple
List of Objective-C 2.0 @ Compiler Directives
- @class
- @defs
- @protocol @required @optional @end
- @interface @public @package @protected @private @property @end
- @implementation @synthesize @dynamic @end
- @throw @try @catch @finally
- @synchronized @autoreleasepool
- @selector @encode
- @compatibility_alias
- @"string"
CMake compile
clang을 사용할 경우 아래와 같이 objective-c를 컴파일 할 수 있다.
cmake_minimum_required(VERSION 2.8)
project(HelloWorld)
set(NAME HelloWorld)
set(CMAKE_C_FLAGS "-x objective-c")
#set(CMAKE_CXX_FLAGS "-x objective-c++")
set(HEADER_FILES
./uhdplayerengine/HelloWorld/HelloWorld/AppDelegate.h
)
set(SOURCE_FILES
./uhdplayerengine/HelloWorld/HelloWorld/AppDelegate.m
./uhdplayerengine/HelloWorld/HelloWorld/main.m
)
set(XIB_FILE
./uhdplayerengine/HelloWorld/HelloWorld/Base.lproj/MainMenu.xib
)
add_executable(
${NAME}
MACOSX_BUNDLE
${HEADER_FILES}
${SOURCE_FILES}
${XIB_FILE}
)
set_source_files_properties(
${XIB_FILE}
PROPERTIES
MACOSX_PACKAGE_LOCATION
Resources
)
set_target_properties(
${NAME}
PROPERTIES
MACOSX_BUNDLE_INFO_PLIST
./uhdplayerengine/HelloWorld/HelloWorld/HelloWorld-Info.plist
)
target_link_libraries(${NAME}
"-framework Cocoa"
"-framework AppKit"
"-framework CoreData"
"-framework Foundation"
)
Linker플래그를 전달 할 때 Foundation
을 -lFoundation
으로 인식할 경우 -Wl,-framework,Foundation
과 같이 전달하면 된다.
Framework
- Foundation
- AVFoundation
- CoreVideo
- CoreMedia
- CoreGraphics
- OpenGL
- CoreImage
- AppKit
- CoreFoundation
- Security
- AudioToolbox
- VideoToolbox
- CoreServices
- CGDirectDisplay
Memory management
Objective-C의 메모리 관리 방법에 대한 설명.
Memory management: Favorite site
- 문씨의 강좌: 메모리 관리 Objective-C
- ObjC 객체의 retainCount 이야기
- [추천] OBJECTIVE-C의 메모리 관리 방법 1
- iOS 메모리 관리: retain/release/autorelease
Examples
문자열 포맷팅
NSString 클래스의 stringWithFormat
함수 사용.
int i = 1;
NSString *name = @"thefirstsong";
NSString *filename = [NSString stringWithFormat:@"%0d-%@.mp3", i, name];
NSString 와 CFStringRef 상호 변환
Typically:
and
Now, if you want to know why the __bridge
keyword is there, you can refer to the Apple documentation. There you will find:
-
__bridge
transfers a pointer between Objective-C and Core Foundation with no transfer of ownership. -
__bridge_retained
or CFBridgingRetain casts an Objective-C pointer to a Core Foundation pointer and also transfers ownership to you. You are responsible for calling CFRelease or a related function to relinquish ownership of the object. -
__bridge_transfer
or CFBridgingRelease moves a non-Objective-C pointer to Objective-C and also transfers ownership to ARC. ARC is responsible for relinquishing ownership of the object.
- NSString → CFString
:
// Don't transfer ownership. You won't have to call `CFRelease`
CFStringRef str = (__bridge CFStringRef)string;
// Transfer ownership (i.e. get ARC out of the way). The object is now yours and you must call `CFRelease` when you're done with it
CFStringRef str = (__bridge_retained CFStringRef)string // you will have to call `CFRelease`
- CFString → NSString
:
// Don't transfer ownership. ARC stays out of the way, and you must call `CFRelease` on `str` if appropriate (depending on how the `CFString` was created)
NSString *string = (__bridge NSString *)str;
// Transfer ownership to ARC. ARC kicks in and it's now in charge of releasing the string object. You won't have to explicitly call `CFRelease` on `str`
NSString *string = (__bridge_transfer NSString *)str;
CFString to C string
const char *cs = CFStringGetCStringPtr( cfString, kCFStringEncodingMacRoman ) ;
puts( cs ) ; // works
Sample
- MotionSensorManager (Geosoft iOS MyCarNavi)
-
MotionSensorManager.zip - iPhone의 모션센서 데이터 취득 클래스.
See also
Favorite site
- Wikipedia (en) Objective-C에 대한 설명
- Windows + GNUSTEP + CLANG 사용시 몇 가지 알아 둘 점
- Closure and anonymous functions in Objective-C 2
- Apple - The Objective-C Programming Language 3
- From C++ to Objective-C 4