0Pricing
Objective-C iOS Development for Legacy & Enterprise Apps · 课时

创建 Objective-C 框架

开发并集成可供 Objective-C 和 Swift 项目共同使用的 Objective-C 框架。

创建 Objective-C 框架 是 CoddyKit 上的免费 Objective-C iOS Development for Legacy & Enterprise Apps 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Objective-C iOS Development for Legacy & Enterprise Apps 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Objective-C iOS Development for Legacy & Enterprise Apps 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

What are Frameworks?

Welcome! Today, we'll dive into Objective-C frameworks. Think of a framework as a self-contained bundle of code and resources.

  • They package reusable code, like classes and functions.
  • They can also include assets, such as images or localized strings.
  • Frameworks promote modularity, making it easier to reuse code across multiple projects or distribute to others.

They are essential for building complex apps and sharing components.

Framework vs. Libraries

You might have heard of static or dynamic libraries. How do frameworks differ?

  • Libraries (.a, .dylib) contain only compiled code.
  • Frameworks (.framework) are a directory structure bundling code, headers, resources, and even other frameworks.
  • This self-contained nature simplifies distribution and integration into projects.

On iOS, frameworks are the preferred way to package and share reusable components.

Creating a New Framework

Let's create our first Objective-C framework in Xcode:

  1. Go to File > New > Project...
  2. Select the iOS > Framework & Library template.
  3. Choose Framework and click Next.
  4. Give it a name, e.g., MyUtilityFramework.
  5. Ensure Language is Objective-C.

Xcode will set up a new project tailored for framework development.

Framework Project Structure

After creating your framework, you'll see a structure like this:

  • Public Headers: These headers define the API of your framework. Any classes or methods declared here will be visible to apps using your framework.
  • Private Headers: For internal use only within the framework.
  • Project Headers: Headers used during framework development, not exposed.
  • Umbrella Header: A main header file (e.g., MyUtilityFramework.h) that imports all public headers. This is the single header an app usually imports.

Exposing Framework Classes

To make a class visible outside your framework, its header file must be placed in the 'Public' section of the 'Build Phases > Headers' settings. Let's create a simple Greeter class:

// Greeter.h
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface Greeter : NSObject

- (NSString *)sayHelloTo:(NSString *)name;

@end

NS_ASSUME_NONNULL_END

// Greeter.m
#import "Greeter.h"

@implementation Greeter

- (NSString *)sayHelloTo:(NSString *)name {
    return [NSString stringWithFormat:@"Hello, %@!", name];
}

@end

The Umbrella Header

For users to easily import your framework's public classes, you'll usually import them all into the framework's main umbrella header. This header usually has the same name as your framework (e.g., MyUtilityFramework.h).

Make sure MyUtilityFramework.h includes #import to expose our Greeter class.

// MyUtilityFramework.h (the umbrella header)

#import <Foundation/Foundation.h>

//! Project version number for MyUtilityFramework.
FOUNDATION_EXPORT double MyUtilityFrameworkVersionNumber;

//! Project version string for MyUtilityFramework.
FOUNDATION_EXPORT const unsigned char MyUtilityFrameworkVersionString[];

// In this header, you should import all the public headers of your framework using statements like #import <MyUtilityFramework/PublicHeader.h>

#import <MyUtilityFramework/Greeter.h>

Integrating into Objective-C App

Once your framework is built, you can drag the .framework bundle into an Objective-C project, then embed it in 'General > Frameworks, Libraries, and Embedded Content'.

Then, simply import the umbrella header and use your classes!

// main.m (in an Objective-C app)
#import <Foundation/Foundation.h>
#import <MyUtilityFramework/MyUtilityFramework.h>

int main(int argc, const char * argv[]) {
  @autoreleasepool {
    Greeter *myGreeter = [[Greeter alloc] init];
    NSString *greeting = [myGreeter sayHelloTo:@"CoddyKit User"];
    NSLog(@"%@", greeting);
  }
  return 0;
}

Integrating into Swift App

A major advantage of frameworks is their direct interoperability with Swift. Unlike individual Objective-C files, frameworks do not require a bridging header when used in Swift!

After embedding the framework in your Swift project (same steps as Objective-C), you just import it directly.

Using Framework in Swift

Once imported, you can instantiate and call methods on your Objective-C framework classes directly from Swift code. Xcode automatically generates Swift interfaces for your Objective-C types.

Try running this Swift example!

// main.swift (in a Swift app)
import Foundation
import MyUtilityFramework // Direct import of the framework

let myGreeter = Greeter()
let greeting = myGreeter.sayHello(to: "Swift Learner")
print(greeting)

Frameworks Quick Check

You've learned how to create and use Objective-C frameworks in both Objective-C and Swift projects. Let's test your understanding!

Recap: Objective-C Frameworks

Great job! In this lesson, you've learned:

  • What frameworks are: Self-contained bundles for code and resources.
  • How to create one: Using Xcode's framework template.
  • Structure: Understanding public, private, and umbrella headers.
  • Integration: How to embed and use Objective-C frameworks in both Objective-C and Swift applications.

Frameworks are powerful tools for code reuse and modularity, especially in enterprise environments. Keep building!

常见问题解答

「创建 Objective-C 框架」课时是免费的吗?

是的 — 「创建 Objective-C 框架」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Objective-C iOS Development for Legacy & Enterprise Apps 课程的其余内容,请升级到 CoddyKit PRO。 Objective-C iOS Development for Legacy & Enterprise Apps 课程共包含 4 节课。

「创建 Objective-C 框架」这节课中我会学到什么?

开发并集成可供 Objective-C 和 Swift 项目共同使用的 Objective-C 框架。 你通过在浏览器中直接运行的动手代码来练习 Objective-C iOS Development for Legacy & Enterprise Apps,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Objective-C iOS Development for Legacy & Enterprise Apps 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Objective-C iOS Development for Legacy & Enterprise Apps 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「创建 Objective-C 框架」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Objective-C iOS Development for Legacy & Enterprise Apps 课中编写并运行代码吗?

能。每节 Objective-C iOS Development for Legacy & Enterprise Apps 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 将 Objective-C 桥接到 Swift
  2. 将 Swift 桥接到 Objective-C
  3. 创建 Objective-C 框架
  4. 处理桥接过程中的可空性与类型映射
← 返回 Objective-C iOS Development for Legacy & Enterprise Apps