gpt4 book ai didi

objective-c - Objective-C 中的对象声明

转载 作者:搜寻专家 更新时间:2023-10-30 19:57:15 24 4
gpt4 key购买 nike

除了风格和个人喜好之外,(1)和(2)在Objective-C中声明对象有什么不同吗?

(1) 一行声明、分配、初始化。

Student *myStudent = [[Student alloc] init];

(2) 多行声明、分配、初始化。

Student *myStudent;
myStudent = [Student alloc];
myStudent = [myStudent init];

最佳答案

不,没有区别。 [Student alloc]只是为一个指针分配内存,而[myStudent init]实际上是设置初始值。

如果您熟悉 C,可以将 alloc 视为做

Student *myStudent = calloc(1, sizeof(Student));

并将 init 调用作为设置初始值的函数。

关于objective-c - Objective-C 中的对象声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3011904/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com