Objective C 基础知识
封面图
•
2025-11-23
•
iOS开发
0
0
0
0
Objective-C 是一种面向对象的编程语言,主要用于开发 iOS 和 macOS 应用程序。它是 C 语言的超集,添加了面向对象的特性(如类、对象、继承、多态等)以及动态运行时机制。 以下是一些 Objective-C 的**基础知识**,适合初学者入门: --- ## 一、基本语法结构 ### 1. 注释 ```objective-c // 单行注释 /* 多行注释 */ ``` ### 2. 输出语句 ```objective-c NSLog(@"Hello, World!"); ``` > `NSLog` 是 Objective-C 中用于打印日志的标准函数,`@""` 表示字符串字面量。 --- ## 二、变量与数据类型 ### 1. 基本数据类型 | 类型 | 说明 | |------|------| | `int` | 整数 | | `float` | 浮点数 | | `double` | 双精度浮点数 | | `char` | 字符 | | `BOOL` | 布尔值(`YES` / `NO`) | ### 2. 变量声明 ```objective-c int age = 25; float price = 9.99; BOOL isStudent = YES; ``` --- ## 三、字符串(NSString) ### 1. 创建字符串 ```objective-c NSString *name = @"John"; ``` ### 2. 拼接字符串 ```objective-c NSString *greeting = [NSString stringWithFormat:@"Hello, %@", name]; ``` ### 3. 字符串比较 ```objective-c if ([name isEqualToString:@"John"]) { NSLog(@"Matched"); } ``` --- ## 四、数组(NSArray / NSMutableArray) ### 1. 创建不可变数组 ```objective-c NSArray *fruits = @[@"Apple", @"Banana", @"Orange"]; ``` ### 2. 访问元素 ```objective-c NSString *firstFruit = fruits[0]; ``` ### 3. 可变数组 ```objective-c NSMutableArray *mutableFruits = [NSMutableArray arrayWithArray:fruits]; [mutableFruits addObject:@"Grape"]; ``` --- ## 五、字典(NSDictionary / NSMutableDictionary) ### 1. 创建不可变字典 ```objective-c NSDictionary *person = @{ @"name": @"Alice", @"age": @28 }; ``` ### 2. 访问值 ```objective-c NSString *name = person[@"name"]; NSNumber *age = person[@"age"]; ``` ### 3. 可变字典 ```objective-c NSMutableDictionary *mutablePerson = [NSMutableDictionary dictionaryWithDictionary:person]; [mutablePerson setObject:@"30" forKey:@"age"]; ``` --- ## 六、类和对象 ### 1. 定义一个类(以 `Person` 为例) #### Person.h ```objective-c #import <Foundation/Foundation.h> @interface Person : NSObject @property NSString *name; @property int age; - (void)sayHello; @end ``` #### Person.m ```objective-c #import "Person.h" @implementation Person - (void)sayHello { NSLog(@"Hello, my name is %@ and I am %d years old.", self.name, self.age); } @end ``` ### 2. 使用类 ```objective-c Person *p = [[Person alloc] init]; p.name = @"Bob"; p.age = 30; [p sayHello]; ``` --- ## 七、属性(Properties) ```objective-c @property NSString *name; // 自动生成 getter 和 setter ``` 可以通过 `@synthesize` 自动生成实例变量(在较新版本中可省略)。 --- ## 八、内存管理(ARC) Objective-C 从 Xcode 4.2 开始支持 **自动引用计数(ARC)**,不需要手动调用 `retain`、`release`、`autorelease`。 --- ## 九、协议(Protocol) ### 1. 定义协议 ```objective-c @protocol MyDelegate - (void)didSomething; @end ``` ### 2. 实现协议 ```objective-c @interface MyClass : NSObject <MyDelegate> - (void)didSomething; @end ``` --- ## 十、常见关键字 | 关键字 | 说明 | |--------|------| | `@interface` | 定义类的接口(头文件) | | `@implementation` | 实现类的方法(实现文件) | | `@property` | 声明属性 | | `@synthesize` | 自动生成 getter/setter(ARC 下通常不需要) | | `@autoreleasepool` | 内存管理(在 ARC 中一般由编译器处理) | --- ## 十一、常用框架(Foundation / UIKit) - **Foundation**: 提供基础类(如 `NSString`, `NSArray`, `NSDate` 等) - **UIKit**: 用于构建 iOS 应用界面(如 `UIViewController`, `UIButton`, `UILabel` 等) --- ## 十二、总结 | 主题 | 内容 | |------|------| | 语言类型 | 面向对象,C 的超集 | | 编程风格 | 跟 C 相似,但有 OOP 特性 | | 内存管理 | ARC(推荐)或 MRC(旧) | | 用途 | iOS/macOS 应用开发 | | 学习资源 | Apple 官方文档、Ray Wenderlich、Hacking with Swift | --- 如果你是刚开始学习 Objective-C,建议从 **Xcode** 开发环境开始,尝试写一些简单的控制台应用或 UI 应用。 需要我帮你写一个完整的 Hello World 示例吗?或者想了解某个具体概念?
上一篇:
Flutter 让App支持多语言
标题录入,一次不能超过6条
留言
评论