什么描述最好?真的是什么?

编程入门 行业动态 更新时间:2024-10-09 19:18:45
本文介绍了什么描述最好?真的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

目前我明白它是一种空对象。但是它真的是什么?

Currently I understand it as a kind of "empty object". But what's it really?

推荐答案

Objective-C对象

当你调用这个:

Objective-C objects

First of all, when you call this:

id someObject = [NSArray array];

someObject 但只有一个指针。这意味着,如果 someObject 等于 0x1234 ,那么在内存中的那个地址有一个对象。

someObject isn't the array object directly but only a pointer to it. That means, if someObject is equal to 0x1234 there's an object at that address in the memory.

这是为什么

id someOtherObject = someObject;

不复制对象。两个指针现在指向同一个对象。

doesn't copy the object. Both pointers point now to the same object.

c> nil 定义?让我们来看看源代码:

So, how is nil defined? Let's take a look at the source code:

objc.h

#define nil __DARWIN_NULL /* id of Nil instance */

_types.h

#ifdef __cplusplus … #else /* ! __cplusplus */ #define __DARWIN_NULL ((void *)0) #endif /* __cplusplus */

看起来像 nil 是指向地址0x0的指针。

Looks like nil is a pointer to the address 0x0.

让我们看看 Objective-C编程参考必须说:

将消息发送到nil

在Objective-C中,发送消息到nil是有效的在运行时。有几个模式在Cocoa利用这个的事实。从消息返回到nil的值也可能有效:... Sending Messages to nil

In Objective-C, it is valid to send a message to nil—it simply has no effect at runtime. There are several patterns in Cocoa that take advantage of this fact. The value returned from a message to nil may also be valid: …

返回的值为 nil ,0或 struct ,所有变量初始化为0.哪一个取决于预期的返回类型。在objective-c运行时显式检查 nil 的消息,这意味着它真的很快。

The returned values are either nil, 0 or a struct with all variables initialized to 0. Which one it is depends on the expected return type. There is an explicit check in the objective-c runtime for messages to nil, that means it's really fast.

这3种类型。以下是所有定义:

Those are the 3 types. Here are all the definitions:

#define Nil __DARWIN_NULL /* id of Nil class */ #define nil __DARWIN_NULL /* id of Nil instance */ #define NULL __DARWIN_NULL #define __DARWIN_NULL ((void *)0)

可以看出,它们都完全相同。 Nil 和 nil 由Objective-C, NULL 来自C。

As can be seen, they are all exactly the same. Nil and nil are defined by Objective-C, NULL comes from C.

有什么区别?这只是关于风格。它使代码更具可读性。

What's the difference then? It's only about style. It makes the code more readable.

  • Nil 用作不存在class: Class someClass = Nil 。
  • nil -existent instance: id someInstance = nil 。
  • NULL 到不存在的存储器部分: char * theString = NULL 。
  • Nil is used as a non-existent class: Class someClass = Nil.
  • nil is used as a non-existent instance: id someInstance = nil.
  • NULL is a pointer to a non-existent memory part: char *theString = NULL.

nil 不是一个空对象,而是一个不存在的对象。方法 -getSomeObject 不返回空对象,如果它不存在但返回 nil ,它告诉用户没有对象。

nil isn't an empty object but a non-existent one. A method -getSomeObject doesn't return an empty object if it doesn't exist but returns nil which tells the user that there is no object.

也许这是有道理的:(两者都将编译并运行。)

Maybe this makes sense: (Both would compile and run.)

if (anObject == nil) { // One cannot compare nothing to nothing, // that wouldn't make sense. if (anObject) { // Correct, one checks for the existence of anObject

更多推荐

什么描述最好?真的是什么?

本文发布于:2023-11-27 09:48:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637574.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:的是

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!