【问题标题】:can't copy NSString object to my custom object's property Objective-c无法将 NSString 对象复制到我的自定义对象的属性 Objective-c
【发布时间】:2011-12-22 15:54:23
【问题描述】:

如何将NSString 数据复制到我的自定义对象的NSString 属性? 我有一个关于 NSString *textKDoctor *doctor 属性的对象问题。 KDoctor 是一个具有两个属性的对象:NSString *nameUIImage *photo

self.question.doctor.name=@"abc";
NSLog(@"doctorname: %@", question.doctor.name);

输出是:

医生姓名:(空)

为什么?我该如何解决这个问题?

【问题讨论】:

    标签: iphone objective-c ios nsstring properties


    【解决方案1】:

    我的猜测是question.doctor != self.question.doctor

    question == nil

    question.doctor == nil

    【讨论】:

    • 我已经检查过了。 question.doctor 是 nil :( 但是为什么?-(id)initWithQuestion:(KonsQuestion *)questn { self.question=[[[KonsQuestion alloc] init] autorelease]; question.text=questn.text; return self; } 我的初始化有问题?
    • @Oleg:你没有在我们见过的任何地方设置 question.doctor。
    【解决方案2】:

    您似乎在正确声明时有问题。你是这样定义“问题”属性的吗?

    在您的标题中:

    @class KonsQuestion
    @interface YourClass : NSObject {
      KonsQuestion * _question; 
    }
    @property(nonatomic, retain) KonsQuestion * question;
    

    在实现文件中:

    @implementation YourClass
    @synthesize question = _question
    
    
    @end
    

    在这种情况下,您应该始终使用 self.question 来使用生成的 getter 和 setter,并在 dealloc 方法中使用 [_question release]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-02
      • 2011-07-08
      相关资源
      最近更新 更多