search

Home  >  Q&A  >  body text

objective-c中,对对象赋值是否是为指针赋值?

声明一个类

#import <Foundation/Foundation.h>

@interface Foo : NSObject <NSCoding>

@property(copy, nonatomic)NSString *strVal;
@property int intVal;
@property float floatVal;

@end

在main函数里

#import <Foundation/Foundation.h>
#import "Foo.h"
int main (int argc, char *argv[])
{
    @autoreleasepool {
        Foo *myfoo1 = [[Foo alloc] init];
        Foo *myfoo2;
        
        myfoo1.strVal = @"This is the string";
    }
    return 0;
    
}

这里的strVal既然是一个指向NSString的指针,为什么又可以给它赋值一个NSString对象?

我想大声告诉你我想大声告诉你2758 days ago502

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-05-02 09:23:38

    The modification of NSString is copy, which means that when copying, another piece of memory is opened, not a pointer copy.

    reply
    0
  • PHP中文网

    PHP中文网2017-05-02 09:23:38

    All classes in Objective-c, operations after instantiating objects are pointer operations. @ returns an anonymous object pointer.

    reply
    0
  • Cancelreply