简体   繁体   English

Objective-C 指针和属性

[英]Objective-C pointers and properties

I'm fairly new to C and Objective-C, having been taught Python beforehand.我对 C 和 Objective-C 还很陌生,之前已经学习过 Python。 As such, there are things about the C family that boggle my mind.因此,关于 C 系列的某些事情让我难以置信。 I've looked around the web, gotten books, and browsed the forums here, but some things are still bugging me...我环顾了 web,买了书,浏览了这里的论坛,但有些事情仍然困扰着我……

  1. I understand variables vs. pointers in theory (one returns a value, and the other returns the address of the value in the system.) What I don't understand is when it's appropriate to use one over the other.我在理论上理解变量与指针(一个返回一个值,另一个返回系统中值的地址。)我不明白什么时候适合使用一个而不是另一个。 Any advice?有什么建议吗?

  2. When declaring a class, what is a property?声明 class 时,什么是属性? It seems like properties and class variables are identical, yet I know there must be some critical difference.似乎属性和 class 变量是相同的,但我知道必须有一些关键的区别。

@interface testViewController: UIViewController {

    IBOutlet UILabel *label;
    IBOutlet UIImageView *uiImageView;
 }

 @property (nonatomic, retain) IBOutlet UILable *label;

 @property (nonatomic, retain) IBOutlet UIImageView *uiImageView;

 @end

If possible, although unlikely, could you answer with a comparison to Python?如果可能,尽管不太可能,您能否通过与 Python 的比较来回答? I know Objective-C and Python are night and day, but whatever you can think of would be great.我知道 Objective-C 和 Python 是白天和黑夜,但无论你能想到什么都会很棒。

The books I got were from Apress: Learn C on the Mac, Learn Objective-C on the Mac, and iPhone and iPad Apps for Absolute Beginners.我得到的书来自 Apress:在 Mac 上学习 C,在 Mac 上学习 Objective-C,以及 iPhone 和 iPad 绝对初学者应用程序。

I really do appreciate any help!我真的很感激任何帮助!

  1. I understand variables vs. pointers in theory (one returns a value, and the other returns the address of the value in the system.) What I don't understand is when it's appropriate to use one over the other.我在理论上理解变量与指针(一个返回一个值,另一个返回系统中值的地址。)我不明白什么时候适合使用一个而不是另一个。 Any advice?有什么建议吗?

Unlike Java, Python and .NET where all variables are "pointers to objects" that can be passed around, in C things can exist in two places. Unlike Java, Python and .NET where all variables are "pointers to objects" that can be passed around, in C things can exist in two places.

In the program code (variable appears when code is hit, disappears when function returns).在程序代码中(变量在代码命中时出现,在 function 返回时消失)。 Like this:像这样:

int my_arr[3];

or, on the "heap" which is memory not a part of the program which is requested dynamically like this:或者,在 memory 的“堆”上不是动态请求的程序的一部分,如下所示:

int *my_arr_pointer = malloc(sizeof(int) * 3);

The second example is close to how Java, Python and .NET pass things around.第二个例子接近于 Java、Python 和 .NET 传递事物的方式。 However when you use malloc() to get memory, you need to use free() on it later...or your program sucks up and wastes the computers memory.但是,当您使用 malloc() 获取 memory 时,您需要稍后在其上使用 free() ......否则您的程序会吸收并浪费计算机 memory。 So use technique to ensure you use a consistent amount of memory.因此,使用技术确保您使用一致数量的 memory。 Use the second to write a more flexible application.使用第二个来编写更灵活的应用程序。 C doesn't have memory management which is why the first approach makes a faster, easy to debug program... that requires copying things between functions compared to the second approach where it's easier to have a flexible sized array where the program grows and shrinks in size... but it's slightly more complex to write. C 没有 memory 管理,这就是为什么第一种方法使程序更快、更容易调试的原因……与第二种方法相比,它需要在函数之间复制东西,第二种方法更容易拥有灵活大小的数组,程序可以在其中增长和缩小大小......但写起来稍微复杂一些。 Use what is appropriate.使用合适的。

3.When declaring a class, what is a property? 3.声明class时,什么是属性? It seems like properties and class variables are identical, yet I know there must be some critical difference.似乎属性和 class 变量是相同的,但我知道必须有一些关键的区别。

Properties are wrappers to class variables.属性是 class 变量的包装器。 A "setter" and a "getter".一个“setter”和一个“getter”。 This allows you to:这使您可以:

a) use a breakpoint to find out where in your code that variable is being set. a) 使用断点找出在代码中设置变量的位置。

b) to check security, or permissions, or to validate the value being set. b) 检查安全性或权限,或验证设置的值。

c) A property doesn't have to tie to a variable. c) 属性不必绑定到变量。 It can create an "illusion" of it, when the data is dynamically generated.当数据动态生成时,它可以创建它的“幻觉”。 eg a size property might count the letters in the string when you ask for it, rather than storing it.例如,当您请求时,size 属性可能会计算字符串中的字母,而不是存储它。

d) more flexibility to change the class later without having change everything accessing that property. d) 以后更灵活地更改 class 而无需更改访问该属性的所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM