Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何将值设置为作为其他自定义对象属性的自定义对象属性_Objective C - Fatal编程技术网

Objective c 如何将值设置为作为其他自定义对象属性的自定义对象属性

Objective c 如何将值设置为作为其他自定义对象属性的自定义对象属性,objective-c,Objective C,这就是我所拥有的 @inerface Face : NSObject @property (nonatomic, assign) long idO; @property (nonatomic, assign) NSString *text; @property (nonatomic, assign) Eyes *eyes; @end @interface Eyes : NSObject @property(nonatomic, assign) NSString

这就是我所拥有的

@inerface Face : NSObject    
   @property (nonatomic, assign) long idO;
   @property (nonatomic, assign) NSString *text;
   @property (nonatomic, assign) Eyes *eyes;
@end

@interface Eyes : NSObject
   @property(nonatomic, assign) NSString *color;
   @property(nonatomic, assign) NSNumber *size;
@end
那么我想这样做:

Face *f  = [[Face alloc] init];
f.text = @"trying";
f.eyes.color = @"Blue";
f.eyes.size = 0.4f;
NSLog(@"%@ - %@ - %@ ", f.text, f.eyes.color, f.eyes.size);

但我只得到:“尝试-(空)-(空)”。我如何才能做到这一点?

您需要保留属性的内容

@interface Face : NSObject
@property (nonatomic, strong) long idO;
@property (nonatomic, strong) NSString *text;
@property (nonatomic, strong) Eyes *eyes;
@end

@interface Eyes : NSObject
@property(nonatomic, strong) NSString *color;
@property(nonatomic, strong) NSNumber *size;
@end


@implementation Face
@synthesize idO;
@synthesize text;
@synthesize eyes;
@end

@implementation Eyes 
@synthesize color;
@synthesize size;
@end

您需要保留属性的内容

@interface Face : NSObject
@property (nonatomic, strong) long idO;
@property (nonatomic, strong) NSString *text;
@property (nonatomic, strong) Eyes *eyes;
@end

@interface Eyes : NSObject
@property(nonatomic, strong) NSString *color;
@property(nonatomic, strong) NSNumber *size;
@end


@implementation Face
@synthesize idO;
@synthesize text;
@synthesize eyes;
@end

@implementation Eyes 
@synthesize color;
@synthesize size;
@end

您需要保留内容或存档:

并在NSObject中添加init方法

  - (id)init

  {

  if (self = [super init]) {

    self.eyes = [[Eyes alloc]init];

    return self;


   } else {

    return nil;
    }
  } 

快乐编码;)

您需要保留内容或存档:

并在NSObject中添加init方法

  - (id)init

  {

  if (self = [super init]) {

    self.eyes = [[Eyes alloc]init];

    return self;


   } else {

    return nil;
    }
  } 
快乐编码;)

(null)-(null)因为对象不是init,所以这两个实例变量都为null


试一试

Face *f  = [Face new];
    f.text = @"trying";
    Eyes *eyes = [Eyes new];
    eyes.color = @"Blue";
    eyes.size = @0.4f;
    f.eyes = eyes;
//    f.eyes.color = @"Blue";
//    f.eyes.size = @0.4f;
    NSLog(@"%@ - %@ ", f.text, f.eyes.color)
(null)-(null)因为对象不是init,所以这两个实例变量都为null


试一试

Face *f  = [Face new];
    f.text = @"trying";
    Eyes *eyes = [Eyes new];
    eyes.color = @"Blue";
    eyes.size = @0.4f;
    f.eyes = eyes;
//    f.eyes.color = @"Blue";
//    f.eyes.size = @0.4f;
    NSLog(@"%@ - %@ ", f.text, f.eyes.color)

不要忘记实现中的
@synthesis
;eyes.color=@“蓝色”;眼睛大小=@0.4f;f、 眼睛=眼睛;会有用的是啊@乔伊尔克利福德,你做得很好!非常感谢,请不要忘记实现中的
@synthesis
;eyes.color=@“蓝色”;眼睛大小=@0.4f;f、 眼睛=眼睛;会有用的是啊@乔伊尔克利福德,你做得很好!多谢各位much@Malarkey86您仍然希望将对象属性更改为强引用,否则在再次尝试访问它们时会崩溃。@Malarkey86您仍然希望将对象属性更改为强引用,否则在再次尝试访问它们时会崩溃。