为什么布尔人不用指向一个对象

时间:2021-08-26 22:02:05

When implementing a getter or setter for an NSString it appears as

当实现一个NSString的getter或setter时,它会出现。

-(NSString *)nameOfSomething

however when you get or set a Boolean you don't us an asterisks to point to something

然而,当你得到或设置一个布尔值时,你并没有给我们一个指向某个东西的星号

-(BOOL)nameOfSomething

I was wondering why you have to do this? Shouldn't the asterisks still be needed in order to point? Is there a certain reason a Boolean doesn't need one?

我想知道你为什么要这么做?要指出来,不应该还需要星号吗?是否存在一个布尔值不需要的原因?

Thanks

谢谢

3 个解决方案

#1


4  

It's a good idea to learn C before trying to program iOS, because you're going to be using Objective-C, and Objective-C is C. There is a difference between a plain data type and a pointer. The asterisk has to do with pointers. (Objective-C "object" types are all pointers, but C has other types.)

在编写iOS之前学习C是一个好主意,因为你要使用Objective-C, Objective-C是C。星号与指针有关。(Objective-C“object”类型都是指针,但是C有其他类型。)

The most important aspects of C needed for iOS programming are discussed here:

这里讨论了iOS编程最重要的方面:

http://www.apeth.com/iOSBook/ch01.html

http://www.apeth.com/iOSBook/ch01.html

As that chapter explains, a BOOL is like an NSInteger - it's just a number, not an object.

正如那一章所解释的,BOOL就像一个NSInteger——它只是一个数字,而不是一个对象。

#2


1  

It is a primitive type, just like an int. In theory, it could be an object, but it is so a very simple concept that makes it of almost no use.

它是一种原始类型,就像一个int类型,理论上它可以是一个对象,但它是一个非常简单的概念,几乎没有任何用处。

Consider this analogies. (Left for primitive types, right for classes)

考虑这个类比。(左为基元类型,右为类)

int  : Integer    (Integer from java)
BOOL :  ---
 --- : NSString

and so on.

等等。

If you really require an object, make a wrapper.

如果您真的需要一个对象,那么就做一个包装器。

#3


0  

BOOL are just signed char, you don not take its address/reference.

BOOL只是有符号的字符,你不取它的地址/引用。

You directly assign to it.

你直接分配给它。

If you see its definition which is as :

如果你看到它的定义是:

typedef signed char     BOOL; 
// BOOL is explicitly signed so @encode(BOOL) == "c" rather than "C" 
// even if -funsigned-char is used.

#if __has_feature(objc_bool)
#define YES             __objc_yes
#define NO              __objc_no
#else
#define YES             ((BOOL)1)
#define NO              ((BOOL)0)
#endif

You use to see its value by %d not %@.

您可以使用%d而不是%@来查看它的值。

#1


4  

It's a good idea to learn C before trying to program iOS, because you're going to be using Objective-C, and Objective-C is C. There is a difference between a plain data type and a pointer. The asterisk has to do with pointers. (Objective-C "object" types are all pointers, but C has other types.)

在编写iOS之前学习C是一个好主意,因为你要使用Objective-C, Objective-C是C。星号与指针有关。(Objective-C“object”类型都是指针,但是C有其他类型。)

The most important aspects of C needed for iOS programming are discussed here:

这里讨论了iOS编程最重要的方面:

http://www.apeth.com/iOSBook/ch01.html

http://www.apeth.com/iOSBook/ch01.html

As that chapter explains, a BOOL is like an NSInteger - it's just a number, not an object.

正如那一章所解释的,BOOL就像一个NSInteger——它只是一个数字,而不是一个对象。

#2


1  

It is a primitive type, just like an int. In theory, it could be an object, but it is so a very simple concept that makes it of almost no use.

它是一种原始类型,就像一个int类型,理论上它可以是一个对象,但它是一个非常简单的概念,几乎没有任何用处。

Consider this analogies. (Left for primitive types, right for classes)

考虑这个类比。(左为基元类型,右为类)

int  : Integer    (Integer from java)
BOOL :  ---
 --- : NSString

and so on.

等等。

If you really require an object, make a wrapper.

如果您真的需要一个对象,那么就做一个包装器。

#3


0  

BOOL are just signed char, you don not take its address/reference.

BOOL只是有符号的字符,你不取它的地址/引用。

You directly assign to it.

你直接分配给它。

If you see its definition which is as :

如果你看到它的定义是:

typedef signed char     BOOL; 
// BOOL is explicitly signed so @encode(BOOL) == "c" rather than "C" 
// even if -funsigned-char is used.

#if __has_feature(objc_bool)
#define YES             __objc_yes
#define NO              __objc_no
#else
#define YES             ((BOOL)1)
#define NO              ((BOOL)0)
#endif

You use to see its value by %d not %@.

您可以使用%d而不是%@来查看它的值。