objective-c中变量前的星号,它的真正含义是什么?
我最近在学习Objective-C,我感到困惑:这不是吗objective-c中变量前的星号,它的真正含义是什么?,objective-c,Objective C,我最近在学习Objective-C,我感到困惑:这不是吗 BOOL bl = YES; BOOL *bl_pointer = &bl; *bl_pointer = NO; NSLog(@"bl is %@", bl?@"YES": @"NO"); NSString *st; st = @"ss"; NSLog(@"%@ is a string", st); 像 因为st也是一个指针,就像bl_指针一样,还是我遗漏了什么 “Objective-C中的所有变量都是指针”是什么意
BOOL bl = YES;
BOOL *bl_pointer = &bl;
*bl_pointer = NO;
NSLog(@"bl is %@", bl?@"YES": @"NO");
NSString *st;
st = @"ss";
NSLog(@"%@ is a string", st);
像
因为st也是一个指针,就像bl_指针一样,还是我遗漏了什么
“Objective-C中的所有变量都是指针”是什么意思?语句“Objective-C中的所有变量都是指针”是错误的
所有objc对象都在堆上分配,并通过指针引用(这在技术上也不正确,因为有标记的指针之类的,但在您更好地理解之前,这已经足够好了)
本机C类型(例如,整型和结构)被视为C中的类型
NSString
是一个objective C对象。因此,您需要一个指针
*bl_pointer = NO;
我建议你买一本很好的入门书,因为这些都是非常基本的问题。ObjC是C的超集,所以你必须先理解C。这对我理解星号很有帮助:因此,@“ss”是一个指针,指向一个堆位置,其中包含“ss”的值,@is like&?
@
是一个特殊的objc标记,用于表示objc特殊语法。当与对象一起使用时,它将适当地“装箱”。。。同样,这是一种过于简单化的做法@“某物”产生一个NSString
@1123产生一个NSNumber
。这些对象可能是也可能不是堆上分配的对象,但它们将是指向objc对象的指针。
*bl_pointer = NO;
// This is a C type -- not an objc object.
BOOL bl = YES;
// This is a pointer to a BOOL, initialized to point to bl
BOOL *bl_pointer = &bl;
// Dereference a pointer to assign NO to what is being pointed to
*bl_pointer = NO;
// NSString is an objc object, and variables must be pointers
NSString *st;
// You are now pointing to a NSString with the value "ss"
// The confusion may be due to the @"" syntax, which just means that
// there is some NSString object, that has the value "ss" and it is
// being assigned to st. Note, you read about memory management as well.
st = @"ss";