iOS开发基础-通过C++快速掌握Objective-C语言(基础)

语言范式对比

类型系统

  • Objective-C:动态类型(id类型可指向任何对象)
objc 复制代码
id unknownObj = GetSomeObject(); // 运行时确定具体类型
  • C++:严格静态类型
cpp 复制代码
auto obj = GetSomeObject(); // 编译时类型推导(C++11+)

消息传递机制

  • Objective-C:采用Smalltalk风格动态消息机制,所有方法调用均转换为objc_msgSend运行时函数
objc 复制代码
[obj doSomething:param]; // 编译后等价于objc_msgSend(obj, @selector(doSomething:), param)
  • C++:静态方法调用,编译时确定函数地址
cpp 复制代码
obj->doSomething(param); // 编译时直接绑定虚函数表地址

基本语法差异

main函数

C语言:

c 复制代码
#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

Objective-C:

objc 复制代码
#import <Foundation/Foundation.h>

int main() {
    @autoreleasepool {
        NSLog(@"Hello, World!");
    }
    return 0;
}

区别:

  • 在Objective-C中,我们使用@autoreleasepool来管理内存,类似于C语言中的手动内存管理。
  • NSLog是Objective-C的打印函数,类似C语言中的printf

数据类型

C语言:

c 复制代码
int a = 5;
float b = 3.14;

Objective-C:

objc 复制代码
NSInteger a = 5; // 等同于C语言的int
CGFloat b = 3.14; // 适用于浮动类型

区别:

  • Objective-C使用NSIntegerCGFloat来替代C语言中的基本数据类型,以便更好地支持不同架构。

条件和循环

C语言:

c 复制代码
#include <stdio.h>

int main() {
    int i = 0;
    while (i < 5) {
        printf("i: %d\n", i);
        i++;
    }
    return 0;
}

Objective-C:

objc 复制代码
#import <Foundation/Foundation.h>

int main() {
    @autoreleasepool {
        int i = 0;
        while (i < 5) {
            NSLog(@"i: %d", i);
            i++;
        }
    }
    return 0;
}

区别:

  • 条件语句和循环结构在C和Objective-C中是相同的,唯一的区别是输出方式,C使用printf,Objective-C使用NSLog

类与对象系统

类定义对比

objc 复制代码
// Objective-C(两段式定义)
@interface Vehicle : NSObject
@property (nonatomic) float speed;
- (void)startEngine;
+ (void)registerVehicleType;
@end

@implementation Vehicle
- (void)startEngine { /*...*/ }
+ (void)registerVehicleType { /*...*/ }
@end
cpp 复制代码
// C++(单段式定义)
class Vehicle : public BaseObject {
public:
    float speed;
    void startEngine();
    static void registerVehicleType();
};

核心差异

  • Objective-C使用@interface声明公开接口,@implementation实现细节
  • C++在类声明中直接实现方法(或通过.cpp文件分离)

方法类型对比

  • 实例方法
objc 复制代码
- (void)instanceMethod; // 必须通过对象调用 [obj instanceMethod]

类似C++普通成员函数:obj->memberFunction()

  • 类方法
objc 复制代码
+ (void)classMethod; // 通过类调用 [Vehicle classMethod]

类似C++静态成员函数:Vehicle::staticFunction()

关键区别

  • Objective-C类方法可被子类继承并覆盖(self指向调用类)
  • C++静态方法无多态特性

方法调用与函数调用

C语言: 函数调用直接使用函数名。

c 复制代码
#include <stdio.h>

int add(int a, int b) {
    return a + b;
}

int main() {
    int result = add(3, 5);
    printf("Result: %d\n", result);
    return 0;
}

Objective-C: 在Objective-C中,方法调用使用[]语法。

objc 复制代码
#import <Foundation/Foundation.h>

@interface Calculator : NSObject
- (int)add:(int)a to:(int)b;
@end

@implementation Calculator
- (int)add:(int)a to:(int)b {
    return a + b;
}
@end

int main() {
    @autoreleasepool {
        Calculator *calc = [[Calculator alloc] init];
        int result = [calc add:3 to:5];
        NSLog(@"Result: %d", result);
    }
    return 0;
}

区别:

  • C语言使用函数调用,直接传入参数。
  • Objective-C使用方法调用,语法形式为[对象 方法名: 参数],通过-表示实例方法,+表示类方法。

多标签方法命名

在Objective-C中,方法定义允许为每个参数提供标签。这些标签可以帮助我们在调用方法时明确每个参数的作用。多标签的用法包括:

  1. 方法名中的标签:每个参数都有一个标签,这个标签在方法调用时会提供额外的信息,使代码更具可读性。
  2. 标签的命名约定:标签通常简洁且描述性强,代表参数的意义。

示例1:

objc 复制代码
#import <Foundation/Foundation.h>

@interface Calculator : NSObject
- (int)add:(int)a to:(int)b;
@end

@implementation Calculator
- (int)add:(int)a to:(int)b {
    return a + b;
}
@end

int main() {
    @autoreleasepool {
        Calculator *calc = [[Calculator alloc] init];
        int result = [calc add:3 to:5];
        NSLog(@"Result: %d", result);
    }
    return 0;
}
  • - 表示这是一个实例方法(而不是类方法,类方法用 +)。
  • (int)add: 表示方法的第一个参数 a,它的类型是 int,方法名的一部分是 add:。这个 add: 并不是表示操作符,而是方法的部分名字。
  • to: 是第二个参数的标签(parameter label),它的作用是描述这个参数的意义。这里 to: 表示操作是"从第一个数(a)加到第二个数(b)",是方法名称的一部分。
  • (int)b 是第二个参数 b,它的类型是 int

示例2:

objc 复制代码
- (void)setRect:(CGFloat)x 
              y:(CGFloat)y 
          width:(CGFloat)width 
         height:(CGFloat)height;

// 调用方式
[view setRect:10 y:20 width:100 height:200];

近似C++实现:

cpp 复制代码
void setRect(float x, float y, float width, float height);
view.setRect(10, 20, 100, 200);

设计差异

  • Objective-C强制参数标签提升可读性
  • C++依赖参数顺序和类型
相关推荐
岁月向前18 小时前
Jenkins实现iOS自动化打包
ios
2501_915909062 天前
手机崩溃日志导出的工程化体系,从系统级诊断到应用行为分析的多工具协同方法
android·ios·智能手机·小程序·uni-app·iphone·webview
2501_915106322 天前
App HTTPS 抓包实战解析,从代理调试到真实网络流量观察的完整抓包思路
网络协议·http·ios·小程序·https·uni-app·iphone
要站在顶端2 天前
iOS自动化测试全流程教程(基于WebDriverAgent+go-ios)
开发语言·ios·golang
2501_916008892 天前
深入理解 iPhone 文件管理,从沙盒结构到开发调试的多工具协同实践
android·ios·小程序·https·uni-app·iphone·webview
腾讯云qcloud07552 天前
腾讯位置商业授权iOS 轨迹SDK
macos·ios·cocoa
2501_916007472 天前
没有 Mac,如何在 Windows 上架 iOS 应用?一套可落地的工程方案
android·macos·ios·小程序·uni-app·iphone·webview
2501_915106322 天前
uni-app 上架 iOS 的完整实践,从跨端开发到稳定提交的工程路径
android·ios·小程序·uni-app·cocoa·iphone·webview
2501_916007472 天前
HTTPS工作原理与重要性:全面安全指南
网络协议·安全·ios·小程序·https·uni-app·iphone
函数的彼端3 天前
iOS Model Generator - 让 JSON 转模型变得简单高效
ios·json·cocoa