Skip to content

yuman07/OCCodeBook

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OCCodeBook

An Objective-C code reference book in the form of a runnable iOS project

Stars
Language Platform License

English | 中文


What is OCCodeBook?

OCCodeBook is a practical Objective-C code handbook built as a working iOS project. Instead of static documentation, it provides runnable code examples that demonstrate commonly used Objective-C programming patterns and advanced features — from memory management macros to runtime programming.

Open it in Xcode, browse the source files, and see each pattern in real, compilable context.

Features

  • Weak-Strong MacrosTSWeakify / TSStrongify for safe block capture, plus RUN_BLOCK for nil-safe block invocation
  • Enum & Constant Patterns — Standard NS_ENUM / NS_OPTIONS definitions, FOUNDATION_EXPORT public constants vs static const private constants
  • Singleton Pattern — Thread-safe implementation using dispatch_once
  • Block Handling — Type aliases, blocks as method parameters, and block properties with proper memory management
  • Class Properties@property (class, ...) backed by static variables
  • Associated Objects — Dynamically adding properties to categories via objc_setAssociatedObject / objc_getAssociatedObject, covering object, primitive, and block types
  • KVO (Key-Value Observing) — Context-pointer-based observer dispatch, scalar and mutable array observation, proper cleanup in dealloc
  • GCD (Grand Central Dispatch) — Creating serial / concurrent queues, accessing main / global queues
  • String & Emoji Handling — Safe iteration and truncation of strings containing complex emoji (flags, skin tone modifiers, ZWJ sequences)
  • Runtime Programming — Calling private methods via NSSelectorFromString + methodForSelector: + function pointer casting

Development

macOS only — Xcode is required and only runs on macOS.

Prerequisites

Requirement Minimum Version
macOS 15.6 (Sequoia)
Xcode 26.0

Build Steps

# 1. Clone the repository
git clone https://github.com/yuman07/OCCodeBook.git

# 2. Navigate to the project directory
cd OCCodeBook

# 3. Open the Xcode project
open OCCodeBook.xcodeproj

# 4. In Xcode, select a simulator or device, then press ⌘B to build or ⌘R to run

No third-party dependencies — the project uses only Foundation and UIKit.

Technical Overview

OCCodeBook is a minimal iOS app whose sole purpose is to serve as a browsable code reference. The project deliberately avoids third-party dependencies so that every example demonstrates pure Objective-C / Foundation / UIKit patterns without external noise.

The code is organized into two layers:

  • Model layer (Sources/) — YMObject serves as the central demonstration class. Its header defines macros, enums, constants, block type aliases, and the singleton / class-property / block-property interfaces. The implementation provides the concrete patterns. A separate category (YMObject+AssociatedObject) demonstrates runtime-based property injection using objc_setAssociatedObject with three distinct association types (object, primitive, block).
  • Controller layer (ViewController) — Demonstrates patterns that require a live UIKit context: GCD queue creation, KVO observation with context-pointer dispatch, emoji-safe string operations, and runtime private method invocation via IMP function-pointer casting.

Tech Stack

Category Technology
Language Objective-C, C (gnu11)
Frameworks Foundation, UIKit
Runtime Objective-C Runtime
Build Tool Xcode 26
Target iOS 26.0+

Architecture

graph TD
    subgraph AppLifecycle["App Lifecycle"]
        main["main.m"] -->|"UIApplicationMain"| AD["AppDelegate"]
        AD -->|"scene session config"| SD["SceneDelegate"]
        SD -->|"UIWindow rootViewController"| VC["ViewController"]
    end

    subgraph CoreExamples["Core Examples"]
        YM["YMObject.h/.m"] -->|"@selector-based keys"| AO["YMObject+AssociatedObject"]
    end

    subgraph RuntimeAPIs["Foundation / ObjC Runtime APIs"]
        GCD["dispatch_queue_create\ndispatch_get_main_queue"]
        KVO["addObserver:forKeyPath:\nobserveValueForKeyPath:"]
        RT["methodForSelector:\nNSSelectorFromString"]
        ASSOC["objc_setAssociatedObject\nobjc_getAssociatedObject"]
    end

    VC -->|"creates instance, calls methods"| YM
    VC -->|"reads/writes category properties"| AO
    VC -->|"queue creation & dispatch"| GCD
    VC -->|"context-pointer observation"| KVO
    VC -->|"IMP function pointer cast"| RT
    AO -->|"runtime property storage"| ASSOC
Loading
  • App lifecycle flowmain.m calls UIApplicationMain, which creates AppDelegate. The app delegate configures scene sessions, and SceneDelegate sets ViewController as the window's root view controller. This is standard UIKit boilerplate.
  • ViewController as demo driverViewController instantiates YMObject and contains methods (useQueue, setupKVO, stringEmoji, callPrivateMethod) that each demonstrate a distinct Objective-C pattern. It directly exercises Foundation APIs (GCD, KVO) and Objective-C runtime APIs (IMP casting).
  • YMObject as pattern catalog — The header/implementation pair covers macros, enums, constants, singleton, blocks, and class properties. The private method doSomeThingWithName:age:birthday: exists specifically to be invoked via runtime from ViewController.
  • Category for runtime injectionYMObject+AssociatedObject adds three properties (NSString, NSInteger, block) to demonstrate all common objc_setAssociatedObject usage patterns without modifying the original class.

Project Structure

OCCodeBook/
|-- OCCodeBook.xcodeproj/                    # Xcode project configuration
|-- OCCodeBook/
|   |-- Sources/
|   |   |-- YMObject.h                       # Macros, enums, constants, singleton, blocks, class properties
|   |   |-- YMObject.m                       # Implementations + private method for runtime demo
|   |   |-- YMObject+AssociatedObject.h      # Category interface: object, primitive, block properties
|   |   `-- YMObject+AssociatedObject.m      # Associated object implementations via objc runtime
|   |-- ViewController.h                     # View controller interface
|   |-- ViewController.m                     # GCD, KVO, emoji handling, runtime method calling
|   `-- Support/
|       |-- AppDelegate.h/.m                 # App lifecycle management
|       |-- SceneDelegate.h/.m               # Scene session management
|       |-- main.m                           # Entry point
|       |-- Assets.xcassets/                 # Asset catalog (app icon, accent color)
|       |-- Info.plist                       # App configuration
|       `-- Base.lproj/
|           |-- Main.storyboard              # Main UI layout
|           `-- LaunchScreen.storyboard      # Launch screen
|-- README.md                                # English documentation
|-- README_ZH.md                             # Chinese documentation
|-- LICENSE                                  # MIT License
`-- .gitignore

Acknowledgments

License

This project is licensed under the MIT License.

About

A practical Objective-C cookbook in a runnable iOS project | Objective-C 实用代码速查手册,以 iOS 工程形式收录常用模式与高级特性

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors