Objective-C
lastmod 2021-04-04created 2014-02-0434 edits
|
The Objective-C language (often written ObjC) is an object-oriented language that adds Smalltalk-style messaging syntax to the C programming language.
Currently, the language is used in Mac OS X, the operating system of Apple's Macintosh, and in iOS, the operating system of the iPhone. Objective-C is the primary language for using Apple's Cocoa, and was originally the main language of NeXT's NeXTSTEP operating system. Generic Objective-C does not use the libraries mentioned above.
Basic
- (Repost) Basic syntax of Objective C
- ObjectiveC:DefaultClassTemplate: How to write a basic Objective-C class.
- XCode
- XCode:iPhone:Basic - Basics of iPhone app development.
- 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
When using clang, you can compile objective-c as follows.
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"
)
When passing Linker flags, if Foundation is interpreted as -lFoundation, pass it as -Wl,-framework,Foundation.
Framework
- Foundation
- AVFoundation
- CoreVideo
- CoreMedia
- CoreGraphics
- OpenGL
- CoreImage
- AppKit
- CoreFoundation
- Security
- AudioToolbox
- VideoToolbox
- CoreServices
- CGDirectDisplay
Memory management
Explanation of how memory is managed in Objective-C.
Memory management: Favorite site
- Moon's lectures: Memory management Objective-C
- About the retainCount of ObjC objects
- [Recommended] How memory is managed in OBJECTIVE-C 1
- iOS memory management: retain/release/autorelease
Examples
String formatting
Use the stringWithFormat function of the NSString class.
int i = 1;
NSString *name = @"thefirstsong";
NSString *filename = [NSString stringWithFormat:@"%0d-%@.mp3", i, name];
Converting between NSString and 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:
-
__bridgetransfers a pointer between Objective-C and Core Foundation with no transfer of ownership. -
__bridge_retainedor 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_transferor 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 - Class for acquiring iPhone motion sensor data.
See also
Favorite site
- Wikipedia (en) explanation of Objective-C
- A few things to know when using Windows + GNUSTEP + CLANG
- Closure and anonymous functions in Objective-C 2
- Apple - The Objective-C Programming Language 3
- From C++ to Objective-C 4